Regular expression with double quotes

Given the line between quotes, as such "Hello"

The following regex will print a string match without double quotes:

/"([^"]+)"/

      

I don't understand how he captures the characters. I believe this should be a capture - this is just an initial double quote. What this regex says is to find an expression that starts and ends with double quotes, and again has one or more double quotes at the beginning. And it captures that one or more double quotes at the beginning. How does this end up matching the line here with [^"]+

?

+3


source to share


1 answer


Expression [^"]+

means literally match all characters that are not double quotes "

. So when it is placed inside ()

, all characters following the first "

and up to the next "

are captured. This is because ^

inside a character class []

implies negation, not the beginning of a line, as that would mean outside []

. So, [^"]

literally means anything other than "

.

It ()

is itself a capturing group, and the regex will only capture the expression that exists inside ()

. Depending on the programming language you are using, it may also write the entire string matched "Hello"

by the whole expression /"([^"]+)"/

in a separate variable, but the purpose ()

is to contain it.



Full expression breakdown:

  • "

    - the first literal quote
  • (

    - start recording
  • [^"]+

    all subsequent characters up to but not including "

  • )

    - end capture group
  • "

    - final closing quote literally
+8


source







All Articles