Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re^3: How to use "less than" and "greater than" inside a regex for a $variable number by AnomalousMonk
in thread How to use "less than" and "greater than" inside a regex for a $variable number by Polyglot

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2017-12-15 05:34 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (417 votes). Check out past polls.