Front page | perl.beginners |
Postings from February 2002
Re: Regex...
Thread Previous
From:
Tanton Gibbs
Date:
February 27, 2002 15:59
Subject:
Re: Regex...
Message ID:
02f001c1bfe8$ff5baac0$61486e0a@brooklyn
I would probably do something like:
my $file;
my %values;
# open the text file and read in all contents (eliminate new lines)
.... #elided for space
# now search
my @patterns( "Pre-paid hours", "Hours used so far",
"Excess hours billed at", ... );
foreach( @patterns ) {
if( $file =~ /$_:\s+\$?(\d+(\.\d+)?)/ ) {
$values{$_} = $1;
}
else {
die "Could not find $_\n";
}
}
What this does is search for the patterns listed in the @pattens array
followed by a colon and spaces an optional $, followed by digits and an
optional fractional part. It then will store the numbers in a hash that is
indexed by the pattern...so you could say
print $values{"Pre-paid hours"};
and it would print
744
Tanton
----- Original Message -----
From: "Daniel Falkenberg" <dan@vintek.net>
To: <beginners@perl.org>
Sent: Wednesday, February 27, 2002 6:15 PM
Subject: Regex...
> Hey all,
>
> I am just working on a small text file and was wondering if a regular
> expression would help me here. Basically the text file looks like
> this...
>
> User data query User data query Time utilization for this billing period
> Pre-paid hours: 744 Hours used so far: 29.55 Excess hours billed at:
> $1.00 per hour Traffic utilization for this billing period Pre-paid
> megabytes: 500 Megabytes used so far: 60.52 Excess Megabytes billed
> at: $0.18 per Megabyte
>
> Now I want to extract the following...
>
> 1: All digits after Pre-paid hours: including a period (.).
>
> 2: All digits after Hours user so far: including a period (.).
>
> 3: All digits after Excess Hours billed at: including a period (.).
>
> 4: All digits after Pre-paid megabytes: including a period (.).
>
> 5: All digits after Used so far: including a period (.).
>
> 6: All digits after Excess Megabytes billed at: including a period (.).
>
> Would this be the best way of going about this? If so where do I start
> :)
>
> Regards,
>
> Dan
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>
Thread Previous