I have following regex
[,{:](?!\s*"[\w\s,]+")
And following test data
{" hi ":"hallo", hi : hallo, "hi": hallo,hi: "hallo", hi: { "hallo": wu },hi: "Hallo Trippel, hier, hallo"}
I am confused why my negative lookahead doesnt match "Hallo Trippel, hier, hallo"
at all. He, at least that is how it seems to me, matches only "Hallo Trippel
. That is thrown away, and then he continues with , hier
as next match.
See:
I know that it is the character class at beginning of my regex, that starts matching there. But I dont understand why my negative lookahead doesnt "consume" all what I want him to consume. He stops, like he is in lazy mode.
Expectation: I expected all matches except the last two.
I have following regex
[,{:](?!\s*"[\w\s,]+")
And following test data
{" hi ":"hallo", hi : hallo, "hi": hallo,hi: "hallo", hi: { "hallo": wu },hi: "Hallo Trippel, hier, hallo"}
I am confused why my negative lookahead doesnt match "Hallo Trippel, hier, hallo"
at all. He, at least that is how it seems to me, matches only "Hallo Trippel
. That is thrown away, and then he continues with , hier
as next match.
See:
https://regex101.com/r/w5woLb/1
I know that it is the character class at beginning of my regex, that starts matching there. But I dont understand why my negative lookahead doesnt "consume" all what I want him to consume. He stops, like he is in lazy mode.
Expectation: I expected all matches except the last two.
@anubhava I expected all matches except the last two.
Looks like you are trying to match one of the ,
, {
, :
characters that are outside the double quotes as you don't want to match 2 commas inside the last pair of double quotes.
For this purpose you can use this regex:
[,{:](?=(?:(?:[^"]*"){2})*[^"]*$)
RegEx Demo
Details:
[,{:]
: Match any one of the ,
, {
, :
characters(?=...)
: a lookahead to make sure there are even number of double quotes after above charactersBut I dont understand why my negative lookahead doesnt "consume" all what I want him to consume
Lookahead and lookbehind, collectively called "lookaround", are zero-length assertions. They are called "assertions": they do not consume characters in the string, but only assert whether a match is possible or not.
More information