regexregex-look-ahead

Matching specific characters individually except in specific expression


Let's say I have a string:
cat_dog_ !mouse= <name="Jake_Russell!"> gog+cat

I want to match specific symbols _!=+, but not in <name="Jake_Russell!"> that part of this regex <name=\".+\">. So result should be __!=+

I've tried lookAhead:
(?!<name=\".+\">)([_!=+])
but as a result, it matches symbols in <name="Jack_Russell!"> too.


Solution

  • I think you could try capturing groups, capture part <name=\".+\"> into 1 ignored group, and another group for matched specific symbols.

    Regex patten: (?<ignored_group><name=".+">)|(?<matched_group>[_!=+])

    See demo here