I want to match two separate patterns. I can get the first working but not the second.
The first is that one of [eE]
must be immediately preceded by one of [cfd]
but one or more of G
is optionally allowed between them. For example, the expression [cfd]G*[eE]
works on this text as I would expect:
cexmxcGexcexGGG0cexG0G00cGEgx0xgcex0xcGGGGEgx0xgmcexc
The second pattern I want to match is any occurrence of [eE]
preceded by [^cfd]
even if there are zero, one or more of G
between them. I think something like [^cfd]G*[eE]
should work on a slightly modified text:
cexmxcGexcexGGG0vexG0G00cGEgx0xgcex0xvGGGGEgx0xgmcexc
. I would only expect the ve
and vGGGGE
strings to be matched. However, I am getting the two other G[eE]
patterns matched as well even though they are part of a valid cG*e
matched by the first pattern [cfd]G*[eE]
.
How do I avoid matching the G[eE]
substrings in the second example and why is this happening?
CodePudding user response:
If you only want to match ve
and vGGGGE
you can exclude matching G
om the negated character class as well.
What happens is that [^cfd]
can also match a G
char.
So [^cfd]G*[eE]
can match G, then G*
match optional G chars, and then matches e
or E
which can also match Ge
and GE
[^cfdG]G*[eE]