Professional Web Applications Themes

perl regex question - PERL Miscellaneous

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...

  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. #2

    Default Re: perl regex question

    X-Ftn-To: Chad Williams

    [email]chad_dude[/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

  3. #3

    Default Re: perl regex question

    Matija Papec wrote:
    > X-Ftn-To: Chad Williams
    >
    > [email]chad_dude[/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

  4. #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

  5. #5

    Default Re: perl regex question

    Chad Williams <chad_dude> 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]glennjncf.ca[/email]
    Glenn Jackman Guest

  6. #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

  7. #7

    Default Re: perl regex question

    [email]chad_dude[/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

  8. #8

    Default Re: perl regex question

    On Wed, 10 Sep 2003 17:56:46 -0400, Jeff 'japhy' Pinyan
    <pinyajrpi.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

Similar Threads

  1. regex question
    By Math55 in forum PERL Beginners
    Replies: 19
    Last Post: July 21st, 06:22 PM
  2. Perl Regex and Arrays
    By Kevin Barry in forum PERL Beginners
    Replies: 0
    Last Post: October 12th, 04:54 PM
  3. Why does perl think there's a regex?
    By Chris in forum PERL Miscellaneous
    Replies: 4
    Last Post: September 4th, 04:02 PM
  4. regex diffs between perl 5.6.1 and 5.8.0?
    By Patrick Flaherty in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 18th, 09:03 PM
  5. Regex question...
    By Greg Bacon in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 15th, 09:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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