The capture groups "do not work" because your regex does not match. Your regex does not match because the whitespace in the regex is ignored due to the //x regex modifier. If you wish to use the //x modifier (a good idea in general, IMHO), use or \s to represent a space or whitespace.

Well, a good idea in general the /x modifier? Yes and no, in my humble optinion. Certainly not when it breaks a regex that would otherwise work. And the OP's regex would have worked without this modifier.

I definitely agree that it is very good to make a very complicated regex multiline with comments, etc. with the /x modifier

But for relatively simple regex, it sometimes defeats its own purpose and actually make thinks more complicated than they should be. Even though Perl sort of set the syntax for modern regexes, let's us not forget that many other traditional tools are using regexes, including grep, awk, vi, sed, etc. For them, a space usualkkly stands for a space. Changing this entails some risks. I am not sure that you want to do that systematically. Because it tends to make Perl less legible to a large part of the Unix community that has been using regex for several decades. I would say: do it when you need, don't do it when you don't need.

I happily use the /x modifier when I am using a complicated multiline commented regex, and I love this possibility, but I am not convinced that it is useful for a simlple one-line regex. The case discussed here in this post shows exactly that it can be counter-productive.

Just to make sure how blasphematory my post may be, I just grabbed my copy of Damian Conway's Perl Best Practices, and, yes, he says to always use the /x modifier, I am probably an heretic. Well, I have a lot of admiration and respect for Damian, and I certainly don't want to challenge his authority, but that is one of the few cases (perhaps a dozen or two) where I have to disagree with him. In most cases where I am using simple regexes, I don't want to make them more complex than they should be by adding the /x modifier. Of course I fully agree when the regex becomes hairy.

I should add that, in my programming experience in Perl, I am using very simple regexes very very often (for example to discard quickly useless lines in a file), and more complicated regexes much less often.


In reply to Re^2: capture groups are not working -- why?? by Laurent_R
in thread capture groups are not working -- why?? by hiyall

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.