need help for a regex

Discussion in 'Perl Misc' started by Alex Shi, Mar 7, 2004.

  1. Alex Shi

    Alex Shi Guest

    Hello folks,

    I want to retrieve full name, city name, state and country from
    following string:
    "(Firstname Lastname - Traverse City, MI United States)"

    I think maybe a regex can be a solution. However I don't have
    much knowledge on it. I need help. Thanks in advance!

    Alex Shi
     
    Alex Shi, Mar 7, 2004
    #1
    1. Advertising

  2. Alex Shi <> wrote:

    > I want to retrieve full name, city name, state and country from



    Errr, what if the "country" does not _have_ "states"?

    What if the country's states are not represented with
    two upper case letters?


    > following string:
    > "(Firstname Lastname - Traverse City, MI United States)"
    >
    > I think maybe a regex can be a solution.



    ------------------------------------
    #!/usr/bin/perl
    use strict;
    use warnings;

    $_ = '(Firstname Lastname - Traverse City, MI United States)';

    if ( /\(([^-]*)\s*-\s*([^,]*),\s*([A-Z]{2})\s*([^)]*)/ ) {
    print "name: $1\n";
    print "city: $2\n";
    print "state: $3\n";
    print "country: $4\n";
    }
    ------------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 7, 2004
    #2
    1. Advertising

  3. Alex Shi

    Alex Shi Guest

    Many thanks to Tad, it really works for my situation :))
    However your post didn't come to my ISP's news server. I found it on
    google's news group.

    Alex

    Tad McClellan <> wrote in message news:<>...
    > Alex Shi <> wrote:
    >
    > > I want to retrieve full name, city name, state and country from

    >
    >
    > Errr, what if the "country" does not _have_ "states"?
    >
    > What if the country's states are not represented with
    > two upper case letters?
    >
    >
    > > following string:
    > > "(Firstname Lastname - Traverse City, MI United States)"
    > >
    > > I think maybe a regex can be a solution.

    >
    >
    > ------------------------------------
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > $_ = '(Firstname Lastname - Traverse City, MI United States)';
    >
    > if ( /\(([^-]*)\s*-\s*([^,]*),\s*([A-Z]{2})\s*([^)]*)/ ) {
    > print "name: $1\n";
    > print "city: $2\n";
    > print "state: $3\n";
    > print "country: $4\n";
    > }
    > ------------------------------------
     
    Alex Shi, Mar 7, 2004
    #3
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    715
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,635
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    603
  4. Xah Lee
    Replies:
    1
    Views:
    948
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    775
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page