0

I'm trying to write a regex to capture all substrings in form of +++text+-- in a given input. So an example input could be:

Some text ++ not interesting text ++interesting part1-- and again some more text ++interesting part2-- and more text and ...

which I'd like to extract ++interesting part1-- and ++interesting part2--.

My regex attempts

  1. Obviously ++.*?-- does not work. Because for input above it matches "++ not interesting text ++interesting part1--" which is wrong.

  2. My other broken solution was the following regex ++(?!.*++).*-- which only captures the second instance ("interesting part2").

Any explanation and answer is welcome.


Note: I do want to solve the issue with regex to master it, though I know the problem has easier solution by just looking for ++ and -- indices repeatedly.

Emadpres
  • 3,466
  • 2
  • 29
  • 44
  • 1
    `\+\+(?:(?!\+\+).)*?--`, https://regex101.com/r/qRWHkD/1 – Wiktor Stribiżew Dec 16 '21 at 22:40
  • @WiktorStribiżew I think the brilliant part of this solution is the `(.)*`. But it's not clear to me how negative-lookahead works in a repetition part. What if we swap `negative-lookahead` and `.`, it seems to work, right? – Emadpres Dec 16 '21 at 22:50
  • 1
    See [Tempered Greedy Token - What is different about placing the dot before the negative lookahead?](https://stackoverflow.com/a/37343088/3832970) – Wiktor Stribiżew Dec 16 '21 at 22:53
  • @WiktorStribiżew Thanks, your comment really helped me and I agree with you upon the duplicate flag. However I can't understand why you downvoted. of course I searched for similar questions before posting mine, but finding other questions is not straight-forward. – Emadpres Dec 16 '21 at 22:59

0 Answers0