.NET Regex Negative Lookahead For Upper Case Letters -


trying work expression custom .net application extract zip codes addresses.

the addresses in single line

12345 example street, ny 10019 united states 

used following expression

\d{3,5}-\d{3,5}|\d{5}(?![a-z]{2}) 

but seems fetching both 12345 zip code 10019. considering have mentioned 2 upper case letters in negative lookahead, shouldn't considering zip code preceded 2 letter ny code? doing wrong here?

i using |operator zip codes in 12345-12345 12345 formats

please check regex testing here

you may use lookbehind here:

\d{3,5}-\d{3,5}|(?<=[a-z]{2}\s+)\d{5} 

see regex demo

the (?<=[a-z]{2}\s+) require 2 uppercase letters , 1 or more whitespaces before 5 digits.

to make sure match specified number of digits, may use word boundaries \b:

\b(?:\d{3,5}-\d{3,5}|(?<=[a-z]{2}\s+)\d{5})\b 

see another demo.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -