I think you are misunderstanding what the /x modifier is doing for this regular expression. From perlre:

The /x modifier itself needs a little more explanation. It tells the regular expression parser to ignore whitespace that is neither backslashed nor within a character class. You can use this to break up your regular expression into (slightly) more readable parts. The # character is also treated as a metacharacter introducing a comment, just as in ordinary Perl code. This also means that if you want real whitespace or # characters in the pattern (outside a character class, where they are unaffected by /x), that you'll either have to escape them or encode them using octal or hex escapes. Taken together, these features go a long way towards making Perl's regular expressions more readable. Note that you have to be careful not to include the pattern delimiter in the comment--perl has no way of knowing you did not intend to close the pattern early

That means that is you actually want to match a whitespace charcter in your expression, when using the /x modifier, you need to either backslash it, or use \s.

my @matches = (m/(<p\sclass=g>.*?<a\shref=https?:\/\/)([^<]*?>)(.*?(?= +<p\sclass=g>))/gx); # Should work. my @matches = (m/(<p\ class=g>.*?<a\ href=https?:\/\/)([^<]*?>)(.*?(?= +<p\ class=g>))/gx); # Should also work. my @matches = (m/(<p\s+class=g>.*?<a\s+href=https?:\/\/)([^<]*?>)(.*?( +?=<p\s+class=g>))/gx); # Probably more robust like this.
May the Force be with you

In reply to Re: Regex Extended Comments with lookahead? by JediWizard
in thread Regex Extended Comments with lookahead? by Anonymous Monk

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.