Right fix davorg, but wrong explanation :). Here is the relevant code section again:

if ( $contents =~ /line3/g ) { if ( $contents =~ /(line2-)(\w*)/g ) { print $2; } }

In $contents is the slurped file:
line1-11
line2-12
line3-13
The first if matches 'line3' and returns true. The second match picks up at the position where the first match left (because it has also the /g modifier) and fails (!) because line2 is before line3. So removing the /g modifier on the second 'if' solves the problem as the match is now done from the start of $contents. As a matter of fact, the /g modifier can be left out for both matches.

Some further optimisations I would suggest for this regex:

This then leads to the following code:

if ( $contents =~ /^line3/mo ) { if ( $contents =~ /^line2-(\w*)/mo ) { print $1; # has to be changed as well } }

To clarify the /g modifier a little bit further let's take a look at this code (of course see also perlre and perlop):

my $string = "abcde abcde adcde"; while ($string =~ /cd/og) { print "pos = ", pos $string, "\n"; if ($string =~ /a(.)/ocg) { print "a$1 matched at ",pos $string, "\n"; } }

Here the first match happens in a while loop, but important still in scalar context. The inner matching starts at the position where the first left off as it also has the /g modifier. Then the outer match takes its turn again starting where the inner left off. The position in the string is only reset when a match fails. This does not happen when the /c modifier is given. This is necessary for the second match in this case - otherwise there is an infinite loop.

Taking this code and playing a bit with the modifiers and the string helps a lot in understanding these (not so easy) things. And I haven't even started talking about m//g in list context yet ...

-- Hofmator


In reply to Re: Re: weird regex problem by Hofmator
in thread weird regex problem by toadi

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.