Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression for Zipcode parsing

Reply
Thread Tools

Regular Expression for Zipcode parsing

 
 
offsky@gmail.com
Guest
Posts: n/a
 
      09-21-2005
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

 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      09-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      09-21-2005
(E-Mail Removed) writes:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      09-21-2005
Sherm Pendley <(E-Mail Removed)> writes:

> (E-Mail Removed) writes:
>
>> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
offsky@gmail.com
Guest
Posts: n/a
 
      09-21-2005
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.

 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      09-21-2005
(E-Mail Removed) wrote:

> 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



--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-21-2005
(E-Mail Removed) wrote in news:1127331586.818768.82800
@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=...0code&mode=all

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-21-2005
Brian Wakem <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) wrote:
>
> > 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-21-2005

Tim Hammerquist wrote:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
> > 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*$/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to Format a zipcode into xxxxx-xxxx. Jeff Thur ASP .Net Datagrid Control 1 02-18-2005 04:14 AM
[YO] Free Swingers Search, See Pics, Read Stories, Search Via Zipcode! Swin4Real Mon Computer Support 0 11-23-2004 03:20 AM
[OT] Free Swingers Search, See Pics, Read Stories, Search Via Zipcode! Joe Deluga Computer Support 0 11-17-2004 05:19 AM
ZIPCODE DB ACCESS TO SQL DaveF ASP .Net 6 09-29-2004 11:44 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments