Home > Back-end >  Regex to get string between single or double quotes even if it's empty
Regex to get string between single or double quotes even if it's empty

Time:01-11

Below is the REGEX which I am trying:

/((?<![\\\\])['"])((?:.(?!(?<![\\\\])\\1))*.?)\\1/

Here this is the text which I am giving

val1=""val2>"2022-11-16 10:19:20"

I need blank expressions like for val1 as well, i.e. I need something like below in matches

""
2022-11-16 10:19:20

If I change the text to something like below, I am getting proper output

val2>"2022-11-16 10:19:20"val1=""

Can anyone please let me know where I am going wrong

CodePudding user response:

Use alternatives to match the two cases.

One alternative matches the pair of quotes, the other uses lookarounds to match the inside of two quotes.

""|(?<=")[^"] (?=")

CodePudding user response:

In your pattern, this part (?:.(?!(?<![\\])\1))* first matches any character and then it asserts that what is to the right is not a group 1 value without an escape \

So in this string ""val2>" your whole pattern matches " with the character class ["'] and then it matches " again with the . From the position after that match, it is true that what is to the right is not the group 1 value without a preceding \ and that is why that match is ""val2>" instead of ""


If the second example string does give you a proper output, you could reverse the dot and first do the assertiong in the repeating part of the pattern, and omit matching an optional char .?

Note that the backslash does not have to be in square brackets.

(?<!\\)(['"])((?:(?!(?<!\\)\1).)* )\1

See the updated regex101 demo.

  • Related