named capturing group inside negative lookbehind regex

626 Views Asked by At

I want to avoid catching input as:

:):) but want to catch sa:)ds or simply want to exclude from result if there are two or more of the same tags which are touching one another.

My logic says I need to use negative lookbehind and named capture group but canot make it working and I become unsure if correct way.

I tried with: (?<!(?P<happy>:\)))(?P=happy) so if I have input as :):) --:)-abc I want to match only from second line ":)"

1

There are 1 best solutions below

0
revo On

If you are going to build a regex for .NET don't work with other RegEx engines to test your patterns. That said, you can benefit from variable-length lookbehinds in .NET but not PCRE (engine you're working with).

This would be a workaround in .NET:

(?<happy>:\))(?<!\k<happy>{2,})(?!\k<happy>)

That obviously doesn't work in regex101.com