This is true especially if Z happens not to be just a latter, [sic] but a more complicated pattern.

To expand on moritz's Re^5: This regexp made simpler: This, I think, is exactly the motivation behind regex objects. Using really non greedy match as an example, some regex objects can be factored out and treated as if they were atomic – because they are, more or less! (The big gotcha is that things get tricky if the factored regexes contain capturing groups, which consequently should be avoided. This problem is ameliorated by 5.10's relativistic approach to referencing capture variables.) The 'regex factoring' approach can lead to a lot more initial verbosity, but this cost is repaid many-fold by greater ease in conceptualizing, building and maintaining complex regexes.

In the example below,  $not_S* and  $not_S*? work as one would expect for  .* and  .*? expressions. (There is a problem with the counting quantifiers {n} et al: something like  $not_S{3} looks like a hash element; the more awkward  (?:$not_S){3} must be used instead.)[See update] Note that something like  $S or  $E could be a much more complicated (and factored) pattern.

>perl -wMstrict -le "$_ = 'no a START no b START yes c END maybe d END no e START yes f END'; my $S = qr{ START }xms; my $E = qr{ END }xms; my $not_S = qr{ (?! $S) . }xms; my $Lazy = qr{ $S $not_S*? $E }xms; print qq{'$_'}; print 'greedy: ', map qq{'$_' }, m{ $S $not_S* $E }xmsg; print 'lazy: ', map qq{'$_' }, m{ $S $not_S*? $E }xmsg; print 'compound: ', map qq{'$_' }, m{ $Lazy }xmsg; " 'no a START no b START yes c END maybe d END no e START yes f END' greedy: 'START yes c END maybe d END' 'START yes f END' lazy: 'START yes c END' 'START yes f END' compound: 'START yes c END' 'START yes f END'

Update: Somehow I had the idea that  $scalar{3} in a regex would interpolate like a hash element, but I just tested this in 5.10 and AS 5.8.9 and 'taint so. Where did I get this notion? Update: Ah,  $scalar{'7'} and  $scalar{$n} interpolate like hash elements and  (?:$scalar){$n} looks like a quantifier again; problem solved.


In reply to Re^5: This regexp made simpler by AnomalousMonk
in thread This regexp made simpler by rovf

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!
  • 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, details, 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, summary, 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.