http://www.perlmonks.org?node_id=1037309


in reply to Re: capture groups are not working -- why??
in thread capture groups are not working -- why??

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.

  • Comment on Re^2: capture groups are not working -- why??