0

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 ":)"

revo
  • 47,783
  • 14
  • 74
  • 117
Georgi
  • 107
  • 1
  • 6
  • Try `(?::\)){2,}(*SKIP)(*F)|:\)` – Wiktor Stribiżew Feb 19 '18 at 21:47
  • Can you explain what are you doing with that? – Georgi Feb 19 '18 at 21:49
  • Skipping multiple (2 or more) `:)` and only matching a single `:)`. Is that what you need? – Wiktor Stribiżew Feb 19 '18 at 21:50
  • Might work but I would like to be as I planned with negative lookbehind and name capture group. Reason is that I have in all 4 different groups so I want to use OR operator so using named capture group will be easier and not so long code or in other words I want to describe my groups only 1 time and then re use them – Georgi Feb 19 '18 at 21:55
  • Could you explain the actual problem and provide a fiddle maybe? Please format the question. I think you are looking for [predefined subroutines](http://www.rexegg.com/regex-disambiguation.html#define). – Wiktor Stribiżew Feb 19 '18 at 22:00
  • See https://regex101.com/r/kbI2ao/1 – Wiktor Stribiżew Feb 19 '18 at 22:05
  • No, please use the problem I have described. Your solution is not based on the on I have started so please explain why cannot solve using only negative lookbehind and named capture group. Your solution is also too so easy to break. What if I have input :):):) then it will fail. Thanks, I will check – Georgi Feb 19 '18 at 22:08
  • No idea what you want and why `:):):)` would fail, it won't be matched. Anyway, if I suggest something different is only because your approach will never work because PCRE does not remember captures from negative lookbehinds outside those constructs. – Wiktor Stribiżew Feb 19 '18 at 22:09
  • What language is it? – revo Feb 19 '18 at 22:12
  • Sorry , my mistake. I think actually will do the job. I just need to test if I add multiple groups using OR – Georgi Feb 19 '18 at 22:15
  • just normal regex in https://regex101.com/. After it is ready will be for c# – Georgi Feb 19 '18 at 22:15
  • PCRE and .NET regex flavors differ to some extent, and `(*SKIP)(*F)` is not supported in .NET. Please update the question with real problem, sample inputs with expected outputs, etc. to make your question answerable. If you are not aware of differences across regex libraries, only use a regex tester that is compatible with your target environment. – Wiktor Stribiżew Feb 19 '18 at 22:22

1 Answers1

2

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

revo
  • 47,783
  • 14
  • 74
  • 117