Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone explain what the following pattern checks for???

((\w+)[^\s])+

for the following input
"Y zsddfxdfsW.am i . dddfdfd is though tesing program. Getting Ok Thank YOU. This dog is very friendly."

it sometimes works partially and sometime completely.
Posted

1 solution

Foreword
a) Take a long tour here?: http://www.regular-expressions.info/tutorial.html[^]
b) Install this little tool, and play with it: http://www.weitz.de/regex-coach/[^]

Now the explanation
\w stands for "word character", usually [A-Za-z0-9_]. Notice the inclusion of the underscore and digits.
[^\s] stands for everything but whitespaces
Whilst ()+ means that all this together can be repeated at least once

As you have two character classes consecutively, your matched string needs to have at least two characters. Every odd character has to match the first class, every even character has to match the second one.
There are many substrings in your input that match this (the bold one is the first):
"Y zsddfxdfsW.am i . dddfdfd is though tesing program. Getting Ok Thank YOU. This dog is very friendly."
 
Share this answer
 
Comments
Mounika Bomma 23-Aug-13 4:38am    
Thank you for your explanation it was clear and helpful.
Zoltán Zörgő 23-Aug-13 4:46am    
One note: I have missed the + after \w, thus the explanation is a little different, since it matches many "word characters" before looking for non-whitespaces. But I suppose you got the point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900