• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression  RSS feed

 
Philip Freeman
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output: ksAK12345A(412)555-1212h@l.m412-555-1234412555-1234

Question: Why does output contain characters ksAKhlm ?

regexChecker method:

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\S means non-whitespace character.
k, s, A, K, h, l, m are all non-whitespace characters so they are matched.
 
Philip Freeman
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[^A-Za-z] gets me 12345(412)555-1212@.412-555-1234412555-1234, since i said that i dont want any letters.
\S gets me everything just without whitespaces.

What I dont understand is why when I use these two combined \S[^A-Za-z] do I get back some letters? k, s, A, K, h, l, m.
Isnt johnsmith also a non-space characters so why are they not in the output?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Freeman wrote:[^A-Za-z] gets me 12345(412)555-1212@.412-555-1234412555-1234, since i said that i dont want any letters.


This also gets you the spaces. Remember that a space is not a letter... and since you are matching the spaces, it is the "\S" that is matching the letters before the spaces.

Henry
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to understand that \S[^A-Za-z] doesn't mean anything that is not a whitespace nor a letter (that would be [^\sA-Za-z]).
It means one non-whitespace character followed by one non-letter character.

So it matches:
k_  s_  A_  K_  12  34  5_  A_  (4  12  )5  55  -1  21  2_  h@  l.  m_  41  2-  55  5-  12  34  41  2_  55  5-  12  34

I have replaced spaces with underscores.
 
Philip Freeman
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!