need help for a regex

A

Alex Shi

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
 
T

Tad McClellan

Alex Shi said:
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";
}
 
A

Alex Shi

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 said:
Alex Shi said:
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";
}
------------------------------------
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top