Re: Error : Nested quantifiers before HERE mark in regex



On Nov 29, 2005, at 10:42, Bedanta Bordoloi, Gurgaon wrote:

No, $log_entry[0] holds a string, and we are getting the error as ** is a
part of that string.

Interpolation in regexps is done before the regexp is interpreted as such. For instance, if $foo is "*" and we have


    /.*$foo/

first interpolation is done giving

    /.**/

and that is interpreted as a regexp, which in this case is invalid and would raise an error.

In consequence, metacharacters in interpolated strings need to be escaped to be treated literally, and this is easy to get:

    $str =~ s/(^\Q$log_entry[0]\E\s)//i

There we start escaping with \Q, and end it with \E, so that "\s)" is interpreted as part of the regexp as is. If you wanted to escape the very string outside the regexp there's quotemeta().

-- fxn
.



Relevant Pages

  • Re: Regexp, ***= and subexpressions
    ... specified that the user's input be treated as a literal string rather than ... -exact] in cases where regexp isn't required:P ... Seems to be that regexp can't match word boundaries at "non-word" ... But now Earth has rotated and I can get back to sleep. ...
    (comp.lang.tcl)
  • Re: Regexp: Case-insensitive matching | N factorial
    ... a regexp component that matches a string of letters, ... Will match the string 'cat' anywhere it appears regardless of case. ... Javascript regular expressions have an alternative operator '|' (kind ...
    (comp.lang.javascript)
  • Re: How to get the "longest possible" match with Pythons RE module?
    ... Under most "NFA" implementation, such a regexp is ... searching a very long string that happens not to contain any matches. ... under many implementations it's very easy to end ... double-quote character at each end, and backslash escapes, ...
    (comp.lang.python)
  • Re: Regular Expression confusion
    ... > Christie Taylor wrote: ... That regexp has ... the end of the string will match, regardless of what is at the beginning ... you don't intend a capture, use instead, which is identical ...
    (comp.lang.perl.misc)
  • Re: String search vs regexp search
    ... of occurences using string & regexp. ... I wrote the code for the regexp search as well as the function ... How long is the substring? ...
    (comp.lang.python)