Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need some help on the Regular expression which matches both
1011
1011 NL

I tried the regular expression ^[1-9][0-9]{3}\s?[a-zA-Z]{2}$....and this match 1011 NL but not 1011 ...Please help.

Thanks
Posted
Updated 13-Sep-13 3:49am
v2

Maybe this:
C#
"^[1-9][0-9]{3}\s*(?:[a-zA-Z]{2})?$"
 
Share this answer
 
Comments
vidkaat 13-Sep-13 12:31pm    
Thanks a lot. It works.
phil.o 13-Sep-13 15:09pm    
You're welcome ;)
try this regex:-

var reg = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i;
 
Share this answer
 
Comments
vidkaat 11-Sep-13 10:01am    
Didnt work.
TrushnaK 12-Sep-13 1:52am    
Checkout your postal code on

http://jsfiddle.net/hgU3u/


^ matches beginning of string
[1-9][0-9]{3} matches a single non-zero digit, and three 0-9 digits
? matches 0 or 1 spaces (you could use * to match 0 or more spaces)
(?!sa|sd|ss) is a lookahead test to check that the remainder is not "sa", "sd", or "ss".
[a-z]{2} matches 2 a-z characters
$ matches the end of the string
/i at the end (from sabof's answer below) to simplify this (otherwise you would need to test !sa, !SA, etc... would be unnecessarily lengthy)
vidkaat 12-Sep-13 8:37am    
Thanks for your time. The above Regex match 1011 SD but it didnot match 1011. Please help. I need a Regex which matches both the value 1011 and 1011 SD.

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