in reply to Regular expression with capture following a match

"speed \\w+\"

I don't see how that 'regexp' could capture 'speed'. It only will match successfully if the string contains the literal text "speed\w". That's because you've got a double-backslash in front of the 'w', so it's not functioning as a metacharacter, but rather as a literal character.

As for a regexp that will capture whatever comes after 'speed', how about...

m/speed(.+)/

The regular expression could be made more specific as to what it captures if you define for us what part of the string following 'speed' constitutes "the value next to 'speed'".


Dave

Replies are listed 'Best First'.
Re^2: Regular expression with capture following a match
by ramya2005 (Scribe) on Oct 03, 2005 at 17:14 UTC
    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.

      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