Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You can specify alternatives, and use the (?{}) construct to keep track of how many times those alternatives were required. You have to keep track of how when you backtrack, so you don't count those multiple times (if your regex has that possibility).

The alternatives might allow the regex engine to match less aggressively than you think it ought to, so you could end up with a higher miss rate than you'd like. In the code below, you get an array of the checkpoints that it had to use the alternative. Play with different values of $str.

use strict; my $str = 'PolaBexar'; # Missing letter, extra letter, two capitals my @checkpoints = (); sub stack_checkpoints { my $val = shift; # Remove all checkpoint markers higher than $val @checkpoints = grep { $val>$_ } @checkpoints; print "Backtracking to $val\n"; push @checkpoints, $val; } print "Matched <$&> with ".@checkpoints." misses\n" if $str =~ /(?:p|.{0,1}?(?{stack_checkpoints 0})) (?:o|.{0,1}?(?{stack_checkpoints 1})) (?:l|.{0,1}?(?{stack_checkpoints 2})) (?:a|.{0,1}?(?{stack_checkpoints 3})) (?:r|.{0,1}?(?{stack_checkpoints 4})) (?:b|.{0,1}?(?{stack_checkpoints 5})) (?:e|.{0,1}?(?{stack_checkpoints 6})) (?:a|.{0,1}?(?{stack_checkpoints 7})) (?:r|.{0,1}?(?{stack_checkpoints 8})) /x;
(quick update: made the alternatives non-greedy)

We're not really tightening our belts, it just feels that way because we're getting fatter.

In reply to Re: non-exact regexp matches by Roy Johnson
in thread non-exact regexp matches by vinforget

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
    [Corion]: Hi marto!
    choroba greets
    [choroba]: .org and .com not working for me today, only .net
    Eily is connected on .org

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (8)
    As of 2017-10-23 09:14 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (277 votes). Check out past polls.