Hello Trevor,

On Friday 08 August 2003 00:04, Trevor Morrison wrote:
> HI,
> 
> I am trying to use regex to extract the city,state and zip out of a file.
> Now, the problem is the city can have more then one word to it like
> 
... 
> I have also, bumped into case of 4 or 5 words in the city name!  I am
> looking for a regex expression that will take this into account knowing
> that the form of the line is:

Just a try:

--- SoF testcities.pl
#!/usr/bin/perl

while (<DATA>) {
        chomp;
        $revString = reverse;
        ($revZip, $revCode, $revName) = ( $revString =~ /(\d+)\s+(.*?)\s+(.*)$/ );

        $zip   = reverse $revZip;
        $code  = reverse $revCode;
        $name  = reverse $revName;

        print "$zip - $name ($code)\n";
}

__DATA__
Loves Park IL 61111
APO  AE NY 09012
St. George UT 84770
Columbus OH 43202
Salt Lake City UT 84118
--- EoF testcities.pl


Prints:

61111 Loves Park (IL)
09012 APO  AE (NY)
84770 St. George (UT)
43202 Columbus (OH)
84118 Salt Lake City (UT)


fyi, there's a very nice text from japhy about 'sexeger (or Reverse Regular
Expressions) here: http://japhy.perlmonk.org/sexeger/sexeger.html

I hope this helps...
freddo

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to