Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This should be easy for someone fairly good with regular expressions (one of these things I'm still lacking in).

It needs to match:

Results <b># thru #</b> of #
Of course, # is any number that can have commas in it. After the OF at the end, there can be any number of spaces before the last number. It seems the spaces change from time to time for some reason.

This is my attempt

m/Results <b>[\d,]+ thru [\d,]</b> of \s+ ([\d,])/
On top of that, the last number after the OF must be stored in the varible.

Can someone correct my code?

Replies are listed 'Best First'.
Re: Simple regex with numbers
by Skeeve (Parson) on Sep 29, 2004 at 20:26 UTC
    compare these 2. I think, mine should do what you want
    m/Results <b>[\d,]+ thru [\d,]</b> of \s+ ([\d,])/ m/Results <b>[\d,]+ thru [\d,]+<\/b> of\s+([\d,]+)/
    But I would change it to:
    m#Results\s+<b>[\d,]+\s+thru\s+[\d,]+</b>\s+of\s+([\d,]+)#

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: Simple regex with numbers
by Zed_Lopez (Chaplain) on Sep 29, 2004 at 20:27 UTC

    Try this:

    /Results <b>[\d,]+ thru [\d,]<\/b> of\s+([\d,]+)/

    Or, if this number will always be at the end of the line (save maybe for whitespace):

    /([\d,]+)\s*$/

    Updated: per Skeeve's correction, below, I escaped the / in </b>. (And not only didn't I downvote it, I upvoted it. Corrections: good.)

      -- because you missed a \. Your RE won't work.

      Update: At -1 ;-) Someone can't stand criticism? The missing \ is in the closing bold-tag: You should have used <\/b> instead of </b>

      $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
      Actually, it turns out there mght be a new line after OF and before the last number. So there can be any number of spaces and new lines (which would explain why I can't get any of the examples to work).

      How can I make it also work for newlines?

        \s+ should match new lines, so skeeve's example should still work. Unless you are reading in from a file, and are only reading one line at a time, in which case you would need to slurp the entire file in and then use skeeve's example.

        May the Force be with you
        Not as easy, but solvable without slurping in the whole file. I assume you read from <> to $_
        my $num; while (m#Results\s+<b>[\d,]+\s+thru\s+[\d,]+</b>\s+of\s+([\d,]+)?\s*$# +) { $num=$1; last if defined $1 or eof; $_.=<>; } # value, if any, in $num

        $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: Simple regex with numbers
by TedPride (Priest) on Sep 29, 2004 at 23:24 UTC
    The format of the line could change, so the best way to take what you need is to just go for the number directly following the of:
    $text =~ /of[^\d,]*([\d,]+)/; print $1;