• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression

 
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

Question: Why does output contain characters ksAKhlm ?

regexChecker method:

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic