Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: How to use "less than" and "greater than" inside a regex for a $variable number

by AnomalousMonk (Archbishop)
on Oct 02, 2012 at 05:24 UTC ( [id://996798]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to use "less than" and "greater than" inside a regex for a $variable number
in thread How to use "less than" and "greater than" inside a regex for a $variable number

Actually, I think the problem can be addressed without the need for exotic regex operators or constructs (although this uses the  \K operator introduced with 5.10). Unfortunately, this approach involves the replacement of a substring with the identical substring, an operation that I do not think the regex compiler can optimize away and that therefore may lead to a bit of inefficiency.

>perl -wMstrict -le "my $book = qq{pg. 1 foo pg. 2 bar baz pg. 4 fee fie pg. 5 foe \n} . qq{fum pg. 6 hoo ha pg. 9 deedle pg. 10 \n} . qq{blah blah pg. 14 noddle \n} ; print qq{[[$book]] \n}; ;; my $pn = qr{ pg[.] \s+ }xms; $book =~ s{ $pn (\d+) \K (.*?) (?= $pn (\d+)) } { my $m = missing($1, $3); $m ? qq{$2$m } : $2; }xmsge; print qq{(($book)) \n}; ;; sub missing { my ($i, $j) = @_; ;; return if $j - $i < 2; ;; my ($ii, $jj) = ($i + 1, $j - 1); return $j - $i > 2 ? qq{(pages $ii - $jj missing)} : qq{(page $ii missing)} ; } " [[pg. 1 foo pg. 2 bar baz pg. 4 fee fie pg. 5 foe fum pg. 6 hoo ha pg. 9 deedle pg. 10 blah blah pg. 14 noddle ]] ((pg. 1 foo pg. 2 bar baz (page 3 missing) pg. 4 fee fie pg. 5 foe fum pg. 6 hoo ha (pages 7 - 8 missing) pg. 9 deedle pg. 10 blah blah (pages 11 - 13 missing) pg. 14 noddle ))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://996798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found