Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

by Laurent_R (Canon)
on Jun 05, 2013 at 21:50 UTC ( #1037309=note: print w/replies, xml ) Need Help??

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??

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1037309]
[holli]: so i filed a pull request for a open source project on github, they guy asks me "what motivates me to do this"
[holli]: i am tempted to answer "to please our new asian overlords" or something
[holli]: what a silly question

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-10-18 22:50 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (251 votes). Check out past polls.