Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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'...
    5. Regex question...
      In article <a2b8188a.0307140124.1343aeb6@posting.google.com>, Math55 <magelord@t-online.de> wrote: : hi, i have this regex: : :...
  3. #2

    Default Re: perl regex question

    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]



    --
    Matija
    Matija Papec Guest

  4. #3

    Default Re: perl regex question

    Matija Papec wrote:
    > 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]
    print substr($procspeed, -3);

    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

  5. #4

    Default Re: perl regex question

    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?
    They both match the same thing. The $ anchor doesn't make a difference
    because the .* at the beginning is greedy.


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #5

    Default 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";
    [...]
    > What's the most efficient way to do this? (get the last word/word of
    > digits, etc)
    Also,
    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

  7. #6

    Default Re: perl regex question

    [posted & mailed]

    On 10 Sep 2003, 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?
    They should both result in $procspeed being 450. (They do for me.)
    >What's the most efficient way to do this? (get the last word/word of
    >digits, etc)
    You can use the .* approach, but you need to be sure you write it
    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

  8. #7

    Default Re: perl regex question

    [email]chad_dude@yahoo.com[/email] (Chad Williams) wrote:
    > What's the most efficient way to do this? (get the last word/word of
    > digits, etc)
    Generally slower to faster options:

    $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

  9. #8

    Default Re: perl regex question

    On Wed, 10 Sep 2003 17:56:46 -0400, Jeff 'japhy' Pinyan
    <pinyaj@rpi.edu> wrote:
    >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] );
    I guess index can be left out since list has only one value,
    $last_num = reverse( reverse($procspeed) =~ /(\d+)/ );

    Matija Papec Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139