I need to get rid of unnecessary symbols and text:
{{c1::TEXT TO KEEP}} –> TEXT TO KEEP
{{c1::TEXT TO KEEP::}} –> TEXT TO KEEP
{{c1::TEXT TO KEEP::some text}} –> TEXT TO KEEP
{{c1::FIRST TEXT TO KEEP::some text}} SECOND TEXT TO KEEP {{c2::THIRD TEXT TO KEEP::}} –> FIRST TEXT TO KEEP SECOND TEXT TO KEEP THIRD TEXT TO KEEP
I wrote this regex pattern: \{{2}c\d::(. )(::.*)\}{2}
(replace it with $1
), but it works only for the second and the third cases, but not for the first one and forth.
How to fix my regex pattern to match all three cases? Could anybody help me?
CodePudding user response:
You just need to make the first group non-greedy (. ?)
and the second group optional (::.*)?
.
On top of that, you may want to make sure the second group doesn't consume too much that across the {{...}}
boundary by adding a negative lookahead: (::(?:(?!}}).)*)?
\{{2}c\d::(. ?)(::(?:(?!}}).)*)?\}{2}