Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need a regular expression that should allow only digits and "-" the condition is that the "-" should not come at the first and last position, the "-" should have digit in both sides.
Posted
Updated 15-Nov-10 9:18am
v2
Comments
Toli Cuturicu 15-Nov-10 15:15pm    
Are there many - allowed? (1234-5678-9012 is ok?)

Perhaps regexPattern = @"^\d+(-\d+)*$"; will do a better job...
 
Share this answer
 
v2
Comments
MathewPV 22-Nov-10 23:02pm    
still accepting "-" at the last
Toli Cuturicu 23-Nov-10 9:39am    
Oh, sorry. Just a small correction! Look at the added underlined symbols!
Depending on your meaning...

^(?!-)[-0-9]+(?<!-)$


Or...

^[0-9]+(-[0-9]+)*$


Or...

^((?!--|^-|-$)[-0-9])+$
 
Share this answer
 
v3
 
Share this answer
 

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