简体   繁体   中英

Find and replace parts of matched string in Notepad++

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++.

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 .

在此处输入图片说明

Search on:

'%r'%(XXXX)

Replace with:

Whatever You like \1

\\1 will match the first set of grouping parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM