Home > Archive > PERL Beginners > June 2006 > reg ex problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ryan Moszynski 2006-06-27, 6:57 pm |
| i have this string extracted from a text file i'm writing a program to process:
test_freq = 1.0001;
and i have to extract the "1.0001"
i can't count on the whitspace being where it now is.
I would like to change this line of perl
$getTestFRQ =~ s/\D+//g;
so that instead of killing all non digit characters, it will kill all
non digit characters except for the period.
How do i do this?
thanks, ryan
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2006-06-27, 6:57 pm |
| Ryan Moszynski wrote:
> i have this string extracted from a text file i'm writing a program
> to process:=20
>=20
> test_freq =3D 1.0001;
>=20
> and i have to extract the "1.0001"
>=20
> i can't count on the whitspace being where it now is.
>=20
> I would like to change this line of perl
>=20
> $getTestFRQ =3D~ s/\D+//g;
>=20
> so that instead of killing all non digit characters, it will kill all
> non digit characters except for the period.
>=20
> How do i do this?
>=20
if ( /=3D\s*([\d\.]+)/ ) {
$MyNbr =3D $1;
}
if always with a ; then
if ( /=3D\s*([^;\s]+)/ ) {
$MyNbr =3D $1;
}
The first one would be my choice, but just a thought.
> thanks, ryan
Wags ;)
WGO: x2224
****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************
| |
| Timothy Johnson 2006-06-27, 6:57 pm |
| Here's one example of how you could go about it:
##########################
use strict;
use warnings;
my $testString =3D " test_freq =3D 1.0001;";
print "Test One: ";
if($testString =3D~ /=3D\s*([0-9.]+)\s*;\s*$/){
print $1;
}else{
print "Failed!";
}
print "\n";
##########################
'[0-9.]' is a character class that represents the numbers zero through
nine and the period character.
I added in a few '\s*' matches in case there were spaces before and
after the semicolon. This should be specific enough to match only a
number that is a n rvalue.
-----Original Message-----
From: Ryan Moszynski [mailto:ryan.m.lists@gmail.com]=20
Sent: Tuesday, June 27, 2006 1:27 PM
To: beginners@perl.org
Subject: reg ex problem
i have this string extracted from a text file i'm writing a program to
process:
test_freq =3D 1.0001;
and i have to extract the "1.0001"
i can't count on the whitspace being where it now is.
I would like to change this line of perl
$getTestFRQ =3D~ s/\D+//g;
so that instead of killing all non digit characters, it will kill all
non digit characters except for the period.
How do i do this?
thanks, ryan
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Dr.Ruud 2006-06-27, 6:57 pm |
| "Ryan Moszynski" schreef:
> i have this string extracted from a text file i'm writing a program
> to process:
>
> test_freq = 1.0001;
>
> and i have to extract the "1.0001"
>
> i can't count on the whitspace being where it now is.
>
> I would like to change this line of perl
>
> $getTestFRQ =~ s/\D+//g;
>
> so that instead of killing all non digit characters, it will kill all
> non digit characters except for the period.
>
> How do i do this?
Possible:
s/[^\d.]+//g
but not safe.
Variant:
($getTestFRQ) = /=\s*(\d+\.\d+)/ ;
assuming the string is in $_.
You could also use 'test_freq' as a hash key:
my %h ;
/^\s*(\w+)\s*=\s*(\d+(?:\.\d*)?)/ and $h{lc $1} = $2 ;
(untested)
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| John Ackley 2006-06-27, 6:57 pm |
|
Ryan Moszynski wrote:
> i have this string extracted from a text file i'm writing a program to
> process:
>
> test_freq = 1.0001;
>
> and i have to extract the "1.0001"
>
> i can't count on the whitspace being where it now is.
>
> I would like to change this line of perl
>
> $getTestFRQ =~ s/\D+//g;
>
> so that instead of killing all non digit characters, it will kill all
> non digit characters except for the period.
>
> How do i do this?
>
> thanks, ryan
>
try this
use strict;
use warnings;
my $input = 'test_freq = 1.0001; ';
if($input =~ /([\d\.]+)/) {
print $1,$/;
} else {
print "not found\n";
}
print $1,$/ if $input =~ /([\d\.]+)/;
$input =~ s/[^\d\.]//g;
print $input,$/;
| |
| Ryan Moszynski 2006-06-27, 6:57 pm |
| thanks for the help, that did the trick
On 6/27/06, Ryan Moszynski <ryan.m.lists@gmail.com> wrote:
> i have this string extracted from a text file i'm writing a program to process:
>
> test_freq = 1.0001;
>
> and i have to extract the "1.0001"
>
> i can't count on the whitspace being where it now is.
>
> I would like to change this line of perl
>
> $getTestFRQ =~ s/\D+//g;
>
> so that instead of killing all non digit characters, it will kill all
> non digit characters except for the period.
>
> How do i do this?
>
> thanks, ryan
>
|
|
|
|
|