freelanceprogrammers.org Forum Index » Perl
Regular expression help
Joined: 04 May 2004
Posts: 1
Regular expression help
I am creating a program that will parse a file based on regular
expressions, and am having a little trouble figuring out how to parse
a particular part of it properly.
I have a line that has a city name, a bunch of spaces, and then the
state (2 letter abreviation).
So for example:
DENVER CO
LOS ANGELES CA
I also run into a very similar situation several other times in the
document, where there is a group of miscellaneou character (including
single white spaces), followed by a bunch of spaces
For Example
MS - Mfg. Straigh Time 65.63
Can anyone help me with this regular expression?
CrazzyCodeMonkey
Joined: 05 May 2004
Posts: 16
Regular expression help
It will help to know what you want to do with the information. Do you
only have these two types of records (City-State and Labels-Amount)?
If so, I assume you want to process each one independently: cities and
states are processed as "locations", and labels-amounts as "amounts".
In any case, the following groups each type of record into an array of
arrayrefs. It is up to you to decide waht you want to do with this
info once parsed.
<code>
use Data::Dumper;
while (<DATA>) {
chomp;
if ( /^(.*?)s+([A-Z]{2})$/ ) { # locations
$city = $1;
$state = $2;
push @locations, [ $city, $state];
} elsif ( /^(.*?)s+(d+(.dd)?)$/ ) { # amounts
$label = $1;
$amount = $2;
push @amounts, [$label, $amount];
}
}
print "Locations:
", Dumper(@locations);
print "
Amounts:
", Dumper(@amounts);
__DATA__
DENVER CO
Restrictions Apply
LOS ANGELES CA
MS - Mfg. Straigh Time 65.63
PHOENIX AZ
(Operational expenses - Residual) 45
</code>
I am using the state and the final numeric value as differentiators.
If there are more variants to the records you have to consider them in
the regex.
Hope it helps, and also hope it is not a homework ;)
Rex
--- In perl_official@yahoogroups.com, "Trace" <sinclair@m...> wrote:
> I am creating a program that will parse a file based on regular
> expressions, and am having a little trouble figuring out how to
parse
> a particular part of it properly.
>
> I have a line that has a city name, a bunch of spaces, and then the
> state (2 letter abreviation).
> So for example:
> DENVER CO
> LOS ANGELES CA
>
> I also run into a very similar situation several other times in the
> document, where there is a group of miscellaneou character
(including
> single white spaces), followed by a bunch of spaces
> For Example
> MS - Mfg. Straigh Time 65.63
>
> Can anyone help me with this regular expression?
>
> CrazzyCodeMonkey
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







