Regular Expression for Zipcode parsing

Discussion in 'Perl Misc' started by offsky@gmail.com, Sep 21, 2005.

  1. Guest

    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
     
    , Sep 21, 2005
    #1
    1. Advertising

  2. Brian Wakem Guest

    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
     
    Brian Wakem, Sep 21, 2005
    #2
    1. Advertising

  3. 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
     
    Sherm Pendley, Sep 21, 2005
    #3
  4. Sherm Pendley <> writes:

    > 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
     
    Sherm Pendley, Sep 21, 2005
    #4
  5. Guest

    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.
     
    , Sep 21, 2005
    #5
  6. Brian Wakem Guest

    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
     
    Brian Wakem, Sep 21, 2005
    #6
  7. 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=regex zip code&mode=all

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

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 21, 2005
    #7
  8. Anno Siegel Guest

    Brian Wakem <> wrote in comp.lang.perl.misc:
    > 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.
     
    Anno Siegel, Sep 21, 2005
    #8
  9. Tim Hammerquist wrote:
    > <> 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*$/
     
    William James, Sep 21, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,298
  2. DaveF

    ZIPCODE DB ACCESS TO SQL

    DaveF, Sep 29, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    428
    Scott M.
    Sep 30, 2004
  3. Jeff Thur

    Need to Format a zipcode into xxxxx-xxxx.

    Jeff Thur, Feb 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    186
  4. Jeff Thur
    Replies:
    1
    Views:
    134
    Alvin Bruney [MVP]
    Feb 17, 2005
  5. Irishmaninusa

    Canada ZipCode Finder

    Irishmaninusa, Aug 18, 2003, in forum: ASP General
    Replies:
    1
    Views:
    121
    Aaron Bertrand - MVP
    Aug 18, 2003
Loading...

Share This Page