Regular Expression for Zipcode parsing

O

offsky

I am not very good at regular expressions and I was hoping that someone
could help me out with something that should be fairly simple. I have
a string which contains an address "12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance of a 5
digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

Thanks
 
B

Brian Wakem

I am not very good at regular expressions and I was hoping that someone
could help me out with something that should be fairly simple. I have
a string which contains an address "12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance of a 5
digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

Thanks


That regex just matches "5" when I try it.

you need to put the {5} inside the brackets, and put .* at the beginning to
swallow up as much of the string as possbile before the numeric match.
 
S

Sherm Pendley

I am not very good at regular expressions and I was hoping that someone
could help me out with something that should be fairly simple. I have
a string which contains an address "12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance of a 5
digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

You could use $ to anchor the match to the end:

/([0-9]{5}$/

But you have bigger problems - in the US at least, zip codes aren't always
five digits. Some are nine digits and a dash, in the format "xxxxx-xxxx".

sherm--
 
S

Sherm Pendley

Sherm Pendley said:
I am not very good at regular expressions and I was hoping that someone
could help me out with something that should be fairly simple. I have
a string which contains an address "12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance of a 5
digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

You could use $ to anchor the match to the end:

/([0-9]{5}$/

Oops, left out a closing paren:

/([0-9]{5})$/
But you have bigger problems - in the US at least, zip codes aren't always
five digits. Some are nine digits and a dash, in the format "xxxxx-xxxx".

sherm--
 
O

offsky

Well, Id like to be a little more flexible than requiring that the zip
be the last 5 characters of the string. Because the country could be in
there at the end too.

Brian had a good idea, but I was unable to get it to work. Is this
correct?

/.*([0-9]{5})/

It is still matching the first 5 digit number.
 
B

Brian Wakem

Well, Id like to be a little more flexible than requiring that the zip
be the last 5 characters of the string. Because the country could be in
there at the end too.

Brian had a good idea, but I was unable to get it to work. Is this
correct?

/.*([0-9]{5})/

It is still matching the first 5 digit number.


It matches 92000 for me.

$ perl -e '$str = "12345 Main St. Sometown, CA 92000"; $str =~ m!.*([0-9
{5})!; print "$1\n";'

92000
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g49g2000cwa.googlegroups.com:
I am not very good at regular expressions and I was hoping that someone
could help me out with something that should be fairly simple. I have
a string which contains an address "12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance of a 5
digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

What did you find when you searched CPAN for "regex zip code"?

http://search.cpan.org/search?query=regex zip code&mode=all
 
A

Anno Siegel

Brian Wakem said:
I am not very good at regular expressions and I was hoping that someone
could help me out with something that should be fairly simple. I have
a string which contains an address "12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance of a 5
digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

Thanks

That regex just matches "5" when I try it.

It matches "12345", but only captures "5", the last digit matched. To
be picky.

Anno
 
W

William James

Tim said:
I have a string which contains an address
"12345 Main St. Sometown, CA 92000"
I want a regular expression that will match the last occurance
of a 5 digit number (the zipcode and not the street address).

My best attempt has been /([0-9]){5}/ which matches the first 5 digit
number. Any help would be much appreciated.

You want an anchor. In this case, the '$'.

/(\d{5})\s*$/

This uses \d (which is shorthand for [0-9]). Also, moves the
{5} quantifier inside the parens, so that $1 catches the entire
5-digit number. The /\s*/ accounts for (and discards) any
trailing whitespace that might be in the string. And the
$ causes the regex to match this sequence at the *end* of
a line.

Alternatively, I might just add:

/(\d{5}(?:[-\s]?\d{4}))\s*$/

match both 5-digit and 9-digit zips.

No, that will match only 9-digit zips. It should be

/(\d{5}(?:[-\s]?\d{4})?)\s*$/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top