Negating a backreference in Regular Expressions

Yuval A. picture Yuval A. · Nov 8, 2011 · Viewed 14.7k times · Source

if a string has this predicted format:

value = "hello and good morning"

Where the " (quotations) might also be ' (single quote), and the closing char (' or ") will be the same as the opening one. I want to match the string between the quotation marks.

\bvalue\s*=\s*(["'])([^\1]*)\1

(the two \s are to allow any spaces near the = sign)

The first "captured group" (inside the first pair of brackets) - should match the opening quotation which should be either ' or " then - I'm supposed to allow any number of characters that are not what was captured in the first group, and then I expect the character captured in the group (the enclosing quotation marks).

(the required string should be captured in the second capture-group).
This doesn't work though.

This does:

\bvalue\s*=\s*(['"])([^"']*)["']

but I want to make sure that both the opening and closing quotation mark (either double or single) are the same.


EDIT
The goal was basically to get the opening tag of an anchor that has a certain class-name included within its class attribute, and I wanted to cover the rare occasion of the class attribute including a (') or a (").

Following all of the advices here, I used the pattern:

<\s*\ba\b[^<>]+\bclass\s*=\s*("|'|\\"|\\')(?:(?!\1).)*\s*classname\s*(?:(?!\1).)*\1[^>]*>

Meaning:
Find a tag-open sign.
Allow any spaces.
Find the word a.
Allow any non-closing-tag.
Find "class (any spaces) = (any spaces)"
Get opening quotes, one of the following: (" or ' or \" or \').
From Alan Moore's answer: Allow any characters that are not the opening quotes.
find classname
Allow any characters that are not the opening quotes.
Find the closing quote which is the same as the opening.
Allow any unclosing-tag chars.
Find the closing tag char.

Answer

Alan Moore picture Alan Moore · Nov 8, 2011

Instead of a negated character class, you have to use a negative lookahead:

\bvalue\s*=\s*(["'])(?:(?!\1).)*\1

(?:(?!\1).)* consumes one character at a time, after the lookahead has confirmed that the character is not whatever was matched by the capturing group, (["'']). A character class, negated or not, can only match one character at a time. As far as the regex engine knows, \1 could represent any number of characters, and there's no way to convince it that \1 will only contain " or ' in this case. So you have to go with the more general (and less readable) solution.