I'm having trouble coming up with the regex I need to do this find/replace in Notepad . I would like to find/replace specific character in strings between two words
for example
msgid "flower"
"test hold"
msgstr
I would like to find multiple o
character between msgid "
and msgstr
and replace with a
character. The result is
msgid "flawer"
"test hald"
msgstr
CodePudding user response:
Here I have written regex to perform the action above you have described.
If you replace the o
with whatever you're searching for, you will be able to search and capture it.
This is written assuming the PCRE Regex Engine. (What I could find is the one run in Notepad )
(?<=msgid ")(.*)(o)(.*)(?= msgstr)
Below is the replacement statement. The a
can be replaced with whatever you wish to replace the captured text with. The $1
represents everything after msgid "
and before the search text. The $3
represents anything after the captured statement and before the msgstr
. If you want to use the captured text anywhere in the replace statement, you can reference it with $2
.
$1a$3
CodePudding user response:
This looks very convoluted but should work for all cases:
((?:^.*?msgid "|\G)(?:(?!msgstr)[\s\S])*?)o(?=(?:(?!msgid ")[\s\S])*?msgstr)
Replace with
$1a
See test cases here
Basically what it does is to match o
s after seeing msgid "
, after that get the end of previous match using \G
and continue matching the rest of o
s before encountering msgstr
.