Ask a Question related to PERL Miscellaneous, Design and Development.
-
Chad Williams #1
perl regex question
my $procspeed="state on-line state_begin 1058768026 cpu_type sparcv9
fpu_type sparcv9 clock_MHz 450";
How come this:
$procspeed=~ s/.*\s(\d+)\w*/$1/;
gives me $procspeed=450
but this:
$procspeed=~ s/.*\s(\d+)\w*$/$1/;
matches the whole line?
What's the most efficient way to do this? (get the last word/word of
digits, etc)
TIA
Chad Williams Guest
-
regex question
hi, the regex .* should match everything but newline, right? but it doesnt. sime time ago i read something that .* will not match evrything? is... -
Perl Regex and Arrays
I'm writing a program to parse words of three or more characters from a file into an array. But when I load the words into the array and look to... -
Why does perl think there's a regex?
Dear all, I have a (fairly complicated) script which unmasks a string (or protein sequence, for those who are interested) where it has previously... -
regex diffs between perl 5.6.1 and 5.8.0?
Hi, Back in 5.6.1, the following succeeded in stripping out all x1a garbage chars from a set of files: perl -p0777 -i.bu -e 's/\X1a+$//g'... -
Regex question...
In article <a2b8188a.0307140124.1343aeb6@posting.google.com>, Math55 <magelord@t-online.de> wrote: : hi, i have this regex: : :... -
Matija Papec #2
Re: perl regex question
X-Ftn-To: Chad Williams
[email]chad_dude@yahoo.com[/email] (Chad Williams) wrote:Don't know for most efficient but this is the simplest :)>my $procspeed="state on-line state_begin 1058768026 cpu_type sparcv9
>fpu_type sparcv9 clock_MHz 450";
>
>How come this:
>
>$procspeed=~ s/.*\s(\d+)\w*/$1/;
>
>gives me $procspeed=450
>
>but this:
>
>$procspeed=~ s/.*\s(\d+)\w*$/$1/;
>
>matches the whole line?
>
>What's the most efficient way to do this? (get the last word/word of
>digits, etc)
print ($procspeed =~ /(\d+)/g)[-1]
--
Matija
Matija Papec Guest
-
John Bokma #3
Re: perl regex question
Matija Papec wrote:
print substr($procspeed, -3);> X-Ftn-To: Chad Williams
>
> [email]chad_dude@yahoo.com[/email] (Chad Williams) wrote:
>>>>my $procspeed="state on-line state_begin 1058768026 cpu_type sparcv9
>>fpu_type sparcv9 clock_MHz 450";
>>
>>How come this:
>>
>>$procspeed=~ s/.*\s(\d+)\w*/$1/;
>>
>>gives me $procspeed=450
>>
>>but this:
>>
>>$procspeed=~ s/.*\s(\d+)\w*$/$1/;
>>
>>matches the whole line?
>>
>>What's the most efficient way to do this? (get the last word/word of
>>digits, etc)
>
> Don't know for most efficient but this is the simplest :)
> print ($procspeed =~ /(\d+)/g)[-1]
assuming it is always between 100 and 999.
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
John W. Krahn #4
Re: perl regex question
Chad Williams wrote:
They both match the same thing. The $ anchor doesn't make a difference>
> my $procspeed="state on-line state_begin 1058768026 cpu_type sparcv9
> fpu_type sparcv9 clock_MHz 450";
>
> How come this:
>
> $procspeed=~ s/.*\s(\d+)\w*/$1/;
>
> gives me $procspeed=450
>
> but this:
>
> $procspeed=~ s/.*\s(\d+)\w*$/$1/;
>
> matches the whole line?
because the .* at the beginning is greedy.
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Glenn Jackman #5
Re: perl regex question
Chad Williams <chad_dude@yahoo.com> wrote:
[...]> my $procspeed="state on-line state_begin 1058768026 cpu_type sparcv9 fpu_type sparcv9 clock_MHz 450";Also,> What's the most efficient way to do this? (get the last word/word of
> digits, etc)
my ($last_digits) = $procspeed =~ m{
(\d+) # a sequence of digits
\D*$ # followed by non-digits at the end of the string
}x;
--
Glenn Jackman
NCF Sysadmin
[email]glennj@ncf.ca[/email]
Glenn Jackman Guest
-
Jeff 'japhy' Pinyan #6
Re: perl regex question
[posted & mailed]
On 10 Sep 2003, Chad Williams wrote:
They should both result in $procspeed being 450. (They do for me.)>my $procspeed="state on-line state_begin 1058768026 cpu_type sparcv9
>fpu_type sparcv9 clock_MHz 450";
>
>How come this:
>
>$procspeed=~ s/.*\s(\d+)\w*/$1/;
>
>gives me $procspeed=450
>
>but this:
>
>$procspeed=~ s/.*\s(\d+)\w*$/$1/;
>
>matches the whole line?
You can use the .* approach, but you need to be sure you write it>What's the most efficient way to do this? (get the last word/word of
>digits, etc)
properly. You did, ensuring there's a space before the digits. If you
had left it out, $procspeed would only be '0', since the .* is greedy, and
(\d+) is content in just matching one digit.
I wouldn't suggest the /(\d+)\D*$/ approach, because it requires you try
to match at EACH chunk of digits. If there are many chunks of digits in
your string, that'll be inefficient.
I personally suggest reversing the string, matching the "first" set of
digits, and then reversing that match.
$last_num = reverse( (reverse($str) =~ /(\d+)/)[0] );
Or:
$last_num = reverse $1 if reverse($str) =~ /(\d+)/;
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
Jeff 'japhy' Pinyan Guest
-
Quantum Mechanic #7
Re: perl regex question
[email]chad_dude@yahoo.com[/email] (Chad Williams) wrote:
Generally slower to faster options:> What's the most efficient way to do this? (get the last word/word of
> digits, etc)
$last_word = ($string =~ /(\w+)/g)[-1]; # only keep the last word
$last_word = reverse ( (reverse $string) =~ /(\w+)/ ); # double reverse
$last_word = (split /\W+/, $string )[-1];
$last_word = reverse( (split /\W+/, reverse $string, 2 )[0] );
-QM
Quantum Mechanic Guest
-
Matija Papec #8
Re: perl regex question
On Wed, 10 Sep 2003 17:56:46 -0400, Jeff 'japhy' Pinyan
<pinyaj@rpi.edu> wrote:
I guess index can be left out since list has only one value,>I wouldn't suggest the /(\d+)\D*$/ approach, because it requires you try
>to match at EACH chunk of digits. If there are many chunks of digits in
>your string, that'll be inefficient.
>
>I personally suggest reversing the string, matching the "first" set of
>digits, and then reversing that match.
>
> $last_num = reverse( (reverse($str) =~ /(\d+)/)[0] );
$last_num = reverse( reverse($procspeed) =~ /(\d+)/ );
Matija Papec Guest



Reply With Quote

