Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I am working with regular expressions.It is working great but now i need modification like when the user enter "PartTime" it works well where as when user enters "Part Time" it does not so basically can any body guide me what regular expression might be that allows only strings ("characters") with spaces between words.

Currently my regular expression is
^[a-zA-Z]+$

Regards
Posted
Updated 31-Dec-13 0:57am
v2
Comments
♥…ЯҠ…♥ 31-Dec-13 6:55am    
Space can come at any place right?
loraloper_22 31-Dec-13 7:01am    
yes

Hi,
Its just simple,
Try this

^[a-zA-Z\s]*$

Accepted inputs
part time
Test time
Party time is over


Test this in here[^]

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
loraloper_22 31-Dec-13 7:17am    
wroks like a charm. Thanks a lot
♥…ЯҠ…♥ 31-Dec-13 7:18am    
You are welcome, I dont know why correct working answer is downvoted here? Totally insane
Sergey Alexandrovich Kryukov 31-Dec-13 12:53pm    
I voted 5. It looks like a small group of voters went to do some totally random down-votes during the night. It happens from time to time.
—SA
♥…ЯҠ…♥ 1-Jan-14 22:54pm    
Thanks SA
Check if this works

^[A-Za-z0-9() ]+$

I think your one misses space.

Also you can refer this link which is used to generate Regex.

http://www.hongkiat.com/blog/regular-expression-tools-resources/[^]
I have used expresso its very good.
 
Share this answer
 
There is a special code in regular expressions for whitespaces: it's \w.
So your regular expression should be:
^[a-zA-Z\w]*$
 
Share this answer
 
Comments
loraloper_22 31-Dec-13 7:01am    
I have tried your approach but still not working
if (Regex.IsMatch(mystring, @"^[a-zA-Z\w]*$"))

??
phil.o 31-Dec-13 8:08am    
Yep sorry, I made a mistake, escape character for whitespace is \s instead of \w
♥…ЯҠ…♥ 31-Dec-13 7:04am    
\w denotes number, letter or underscore.So it wont work at all. Try using the one mentioned in my solution

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