Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex negated character class followed by optional character

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].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I avoid matching the G[eE] substrings in the second example and why is this happening?

>Solution :

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]

Regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading