Reputation: 2332
I have something in a text file that looks like '%r'%XXXX
, where the XXXX
represents some name at the end. Examples include '%r'%addR
or '%r'%removeA
. I can match these patterns using regex '%r'%\w+
. What I would like to replace this with is '{!r}'.format(XXXX)
. Note that the name has to stay the same in the replace so I'd get '{!r}.format(addR)
or '{!r}.format(removeA)
. Is there a way to replace parts of the matched string in this way while retaining the unknown variable name pulled out with \w+
in the regex search?
I'm specifically looking for a solution using the find and replace features in Notepad++.
Upvotes: 1
Views: 2074
Reputation: 626861
You can use
'%r'%(\w+)
and replace with '{!r}.format\(\1\)
The '%r'%(\w+)
pattern contains a pair of unescaped parentheses that create a capturing group. Inside the replacement pattern, we use a \1
backreference to restore that value.
NOTE: The (
and )
in the replacement must be escaped because otherwise they are treated as Boost conditional replacement pattern functional characters.
See more on capturing groups and backreferences.
Upvotes: 2
Reputation: 7019
Search on:
'%r'%(XXXX)
Replace with:
Whatever You like \1
\1
will match the first set of grouping parentheses.
Upvotes: 0