Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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~


In reply to How to use "less than" and "greater than" inside a regex for a $variable number by Polyglot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (7)
    As of 2014-04-17 01:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (437 votes), past polls