regex question

  • Thread starter Thread starter mehdi_mousavi
  • Start date Start date
M

mehdi_mousavi

Hi folks,
Consider the following pattern:

(?'city'[\w ]{2,}\b)?\s*,?\s*(?'state'[A-Za-z]{2})\s*(?'zip'\d{5})

This helps me to detect patterns like:

thousand oaks CA, 12345

However, I've got no idea how I am supposed to modify the above pattern
to detect the following address as well:

thousand oaks CA, 12345-1234

Any help would be highly appreciated,

TIA,
Mehdi
 
I am very rusty at regex but this should work: (i just added an
additional match at the end)

(?'city'[\w
]{2,}\b)?\s*,?\s*(?'state'[A-Za-z]{2})\s*(?'zip'\d{5})-(?'plus4'\d{4})


That might not work though, you'll have to give it a shot.
 
This was tough, not so much because it was difficult to write a regular
expression, but because you didn't define your rules clearly.

For example, you didn't say whether or not you are trying to match multiple
addresses in a single string, such as a text file. So, I wrote a regular
expression that can capture multiple addresses in a text file, using the
rule that an address begins at the beginning of a line:

(?m)^(?<city>(?:[\w]+\s)+)\s*[,]?\s*(?<state>\w{2}\b)\s*[,]?\s*(?<zip>[\d]{5}[-]?[\d]{0,4})

The "(?m)" indicates that the caret matches at the beginning of lines as
well as the beginning of the string.

It breaks down this way (I assumed less than you did, so my regex is longer:

(?<city>(?:[\w]+\s)+)\s*[,]?\s*

The "city" group consists of one or more sequences of one or more word
characters (text and digits), followed by a space.

This is followed by 0 or more spaces, followed by 0 or 1 comma, followed by
0 or more spaces.

(?<state>\w{2}\b)\s*[,]?\s*

This is followed by the "state" group, which consists of exactly 2 word
characters followed by a word break.

This is followed by 0 or more spaces, followed by 0 or 1 comma, followed by
0 or more spaces.

(?<zip>[\d]{5}[-]?[\d]{0,4})

This is followed by the "zip" group which consists of exactly 5 digits,
followed by 0 or 1 hyphen, followed by 0 - 4 digits.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 
Back
Top