I want to learn how to capture all occurrences of a character (e.g., -) between (?<=...) and (?=...).
Suppose I have the following text:
- [abc!word1-word2-word3]
- word1-word2-word3
I aim to create a single capture group containing all - only if the string starts with [abc! and ends with ].
I tried the following (e.g., see demo here):
(?<=\[abc!)
.* (-) .*
(?=\])
However, only the last occurrence of - is matched as shown below.
Is there a way to achieve this? For clarity, I am using the PCRE2 flavour with the gmx options.
>Solution :
You can replace each individual hyphen betwee [abc! and the next (closest) ] char using
(?:\G(?!\A)|\[abc!)[^][-]*\K-(?=[^][]*])
See the regex demo.
Details:
(?:\G(?!\A)|\[abc!)– either the end of the previous successful match (\G(?!\A), see this\Greference) or (|)[abc!string (\[abc!)[^][-]*– zero or more chars other than[,]and-\K– a match reset operator that discards the text matched so far from the match memory buffer-– a hyphen(?=[^][]*])– a positive lookahead that makes sure there are zero or more chars other than square brackets followed with a]char immediately to the right of the current location.
