Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to use "less than" and "greater than" inside a regex for a $variable number

by Polyglot (Monk)
on Oct 01, 2012 at 19:56 UTC ( #996728=perlquestion: print w/ replies, xml ) Need Help??
Polyglot has asked for the wisdom of the Perl Monks concerning the following question:

Background: I'm trying to parse some text for page numbers, and some page numbers are missing. The original book may not have had a number on every page, so the skipped pages may be legitimate. However, due to variations in the HTML, some page numbers may have been missed by the parser. So I'm creating a regex to find possible skips so as to tag them properly.

Pages which have already been identified will be tagged in the format of (<a id="GC_2"></a>) where the "GC" is the book code and the number is the page. There is little point in looking for a skipped page out of its range, so I'm trying to match only between these tags where the page should be.

Code example:

my $replace = sub { push @findmissing, "$bookabbrev\t$1\t$2\t$3\n"; return '' }; foreach (0..$#missingpages) { chomp $missingpages[$_]; my $skipped=$missingpages[$_]; my $before=qq|<a id="$bookabbrev\_|.($skipped-1).qq|">|; my $after=qq|<a id="$bookabbrev\_|.($skipped+1).qq|">|; s/$before.*?(.{0,25})(?<!\d)($missingpages[$_])(?>\D)(.{0,25}).*?$afte +r/$replace->()/eg for @source; }
In the above, the "$skipped-1" and "$skipped+1" should really just be something like "<$skipped" and ">$skipped" because there are times when several consecutive pages are skipped, and the code could never hope to find a tag for the one just before or just after each of them. I've looked here and via Google and found that I can use an "if-then" type of expression within a regex, but have found no examples for how to do this. How would you do this?

Here is a sample of the output that gets pushed into @findmissing to help me scan for legitimate missed numbers (neither of these was a page number, as evidenced by the context).

GC kes."--Wylie, b. 16, ch. 1 Did this haughty potenta GC rty."--Wylie, b. 16, ch. 1 This document clearly re
UPDATE: It appears this is not possible in perl. At least, I have not found a way to do this. What I have found is of limited value, but may help me to find the majority of cases...ugly as anything, though. I'm now doing this...
s/ (??{$missingpages[$_]-1|$missingpages[$_]-2|$missingpages[$_]-3|$m +issingpages[$_]-4|$missingpages[$_]-5|$missingpages[$_]-6}) .*? (.{0,30}) (?<!\d) ($missingpages[$_]) (?>\D) (.{0,30}) .*? (??{$missingpages[$_]+1|$missingpages[$_]+2|$missingpages[$_]+3|$m +issingpages[$_]+4|$missingpages[$_]+5|$missingpages[$_]+6}) /$replace->()/egx for @source;
Oh, and this is slow as molasses in January on Jupiter!

Blessings,

~Polyglot~

Comment on How to use "less than" and "greater than" inside a regex for a $variable number
Select or Download Code
Re: How to use "less than" and "greater than" inside a regex for a $variable number
by LanX (Abbot) on Oct 01, 2012 at 20:52 UTC
    don't really think I understand your task, but

    > ...found that I can use an "if-then" type of expression within a regex, but have found no examples for how to do this. How would you do this?

    see perlretut#Conditional-expressions

    HTH

    Cheers Rolf

      I appreciate the link, but that resource only tells how to work with a known quantity. I need to be able to match a variable number, conditional upon its relative value when compared to another number. In essence, I need to match based on the comparison result.

      For example, how would one do something like this?

      $string = "I have 5 apples, 6 oranges, and 8 limes."; #Match the oranges only if they are more than the apples #and fewer than the limes. $string =~ m/(\d+)\sapples.*(\d+)\soranges.*(\d+)\slimes(?{if (($1<$2) + && ($2<$3))})/g;
      If I had something like that I could then plug in $var for $2.

      Blessings,

      ~Polyglot~

        The  (*F) operator was introduced with 5.10. Prior to that,  (?!) can be used.

        >perl -wMstrict -le "for my $n (4 .. 9) { my $str = qq{I have 5 apples, $n oranges, and 8 limes.}; print qq{'$str'}; next unless $str =~ m{ (\d+) \s+ apples \D+ (\d+) \s+ oranges \D+ (\d+) \s+ limes (?(?{ $1 < $2 && $2 < $3 }) | (*F) ) }xms; print qq{'$2'}; } " 'I have 5 apples, 4 oranges, and 8 limes.' 'I have 5 apples, 5 oranges, and 8 limes.' 'I have 5 apples, 6 oranges, and 8 limes.' '6' 'I have 5 apples, 7 oranges, and 8 limes.' '7' 'I have 5 apples, 8 oranges, and 8 limes.' 'I have 5 apples, 9 oranges, and 8 limes.'

        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
Node Status?
node history
Node Type: perlquestion [id://996728]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-04-20 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls