Negating a backreference in Regular Expressions

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.

Answers:

Answer

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.

Answer

You can use:

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

See it

Answer

Without knowing what you need the information for (or indeed even what language or tool you are using this regex in), there are many paths I can suggest.

Using these strings:

value = "hello and good morning"
value = 'hola y buenos dias'
value = 'how can I say "goodbye" so soon?'
value = 'why didn\'t you say "hello" to me this morning?'
value = "Goodbye! Please don't forget to write!"
value = 'Goodbye! Please don\'t forget to write!'

this expression:

"((\\"|[^"])*)"|'((\\'|[^'])*)'

will match these strings:

"hello and good morning"
'hola y buenos dias'
'how can I say "goodbye" so soon?'
'why didn\'t you say "hello" to me this morning?'
"Goodbye! Please don't forget to write!"
'Goodbye! Please don\'t forget to write!'

It would allow either the "other" type of quote or the same type of quote, when escaped with a single preceding \. The contents of the quoted strings are either in group 1 or 3. You could figure out which type of quotes are used by getting the first (or last) character.

If you need some of these things to be in particular match groups, please give more specific examples (and include things that should not work, but look like they might be close)

Please ask if you would like to take this route and need a little more help

Answer

Answering this question How to use a numerical reference in neglected set?

here because it was marked as an exact duplicate of this one.

Can't really specify a capture group inside a class.
What can be done is to specify the character in a negative assertion, like this

(["'])((?:(?!\1)[\S\s])*)(\1)

Expanded

 ( ["'] )                      # (1)
 (                             # (2 start)
      (?:
           (?! \1 )
           [\S\s] 
      )*
 )                             # (2 end)
 ( \1 )                        # (3)

Notice that on the original post [^char] normally matches linebreaks
as well, but since this is JavaScript (the old JS) the dot cannot be used.
Use [\S\s] instead, which matches any character.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.