in reply to Regex weirdness?

I think the problem is in [^\2] - you can't interpolate matches into character classes this way. One way to get around that is to use a negative lookeahead instead.

Also, this will have problems with 'Don\'t do this', since the backslash will be matched by the character class, which means the escaped quote ends up terminating the string. Check for backslashes first to avoid that:

m{( [^"']+ | (["']) (?: \\ . | (?!\2) . )* \2 )}xgs

Hugo

Replies are listed 'Best First'.
Re^2: Regex weirdness?
by Pic (Scribe) on Mar 15, 2005 at 01:42 UTC
    Hmm. That regex looks far better than mine. I really should learn to use the /x modifier when dealing with regexes of a certain complexity. And yeah, the quote problem has occured to me as being a problem (which is why the linear approach is looking more and more appealing TBH).
    Also, that regex seems to have the problem with recapturing the closing quote I had, as well as getting errors about using undefined values in a match (which Isuppose is due to \2 being unset in the first branch).