in reply to Re: Regular expression with capture following a match
in thread Regular expression with capture following a match

You are right Dave.
I actually had the reg ex speed (\\w+) in my code, which captured the string 'speed'.
While typing in the Perl Monks I posted it as speed \\w+\" which was actually wrong.

Replies are listed 'Best First'.
Re^3: Regular expression with capture following a match
by davido (Cardinal) on Oct 03, 2005 at 19:45 UTC

    The regexp...

    m/speed (\\w+)/

    ...won't capture 'speed'. I don't know where you keep coming up with that idea. If that's how your regular expression looked, it's a mistake. Here is why:

    • Parenthesis mark matches that should be captured. The regexp you have shown is not trying to capture 'speed '. It's trying to capture '\\w+'.
    • \\w+ is still a mistake. The metacharacter that directs the regular expression engine to capture a word-like character is '\w'. When you add the second backslash, as in '\\w' you are directing the regular expression parser to treat that subexpression as literal characters, not a special metacharacter. The first backslash essentially 'escapes' the second one. The result is that you are asking the RE engine to match the literal characters '\w', and also telling the RE engine (via the + quantifier) that there may be one or more 'w' characters. You must remove the double-backslash, leaving only one backslash as in speed (\w+). But that still won't capture 'speed', it will capture what comes after speed, if it's a word grouping.

    Please read perlrequick and perlop. In perlop you'll want to read the gory details of quote and quote-like parsing. It may take awhile to sink in, but will help to understand the problem you're having with the double backslash.


    Dave