Notepad++ Regex: Match all occurrences after another match
How to match all occurrences after another match?
For example: I want to replace all - after abc with space.
Input:
one-two-abc-three-four-five
six-seven-abc-eight-nine-ten
...Output:
one-two-abc three four five
six-seven-abc eight nine ten
...In Javascript is possible to use this regex: (?<=(.+abc.+))-, but the Positive Lookbehind seems not to be supported in Notepad++
Thanks.
PS: Feel free to correct the Title. I did my best, but I'm not native speaker.
12 Answers
This will work with any number of hyphens in the string.
- Ctrl+H
- Find what:
(?:^.*abc|\G[^-\r\n]+)\K- - Replace with: # a single space
- CHECK Match case
- CHECK Wrap around
- CHECK Regular expression
- UNCHECK
. matches newline - Replace all
Explanation:
(?: # non capture group ^ # beginning of line .* # 0 or more any character but newline abc # literally abc | # OR \G # restart from last match position [^-\r\n]+ # 1 or more any character that is not hyphen or line break
) # end group
\K # forget all we have seen until this position
- # hyphenScreenshot (before):
Screenshot (after):
1I found this solution.
Find:
^(.+abc)(-([^-]*))?(-([^-]*))?(-([^-]*))?(-([^-]*))?(-([^-]*))?(-([^-]*))?$
Replace:
$1 $3 $5 $7 $11 $13These caveats you need to be aware of:
- All wanted (replaced) occurrences of
-are after the last occurrence ofabc. (-([^-]*))?part of regex is repeated equally or more than max. count of-in a single line.- There will be extra spaces on end of each line which has less
-occurrences than(-([^-]*))?count in the regex - Number after
$in replace expression is start by 1 and is +2 for each(-([^-]*))?occurrence.
Try this regex online at