Syntax for a replace with backreference - example

Mar 5, 2014 at 10:32 AM
Edited Mar 5, 2014 at 10:42 AM
It took me some time to figure it out and since I couldn't find example here, it's better to share.

In FIND we will use grouping construct and capture the matched subexpression into a named group (match subexpression items and put the result in the named group 'name'):

(?<name> subexpression)

In REPLACE we make a reference to the named group, putting it in braces (curly brackets):

${name}

EXAMPLE:

We have multi-line text file and each line has part enclosed in parentheses (inside round brackets). We need to format / wrap in tags only the text enclosed in parentheses:

BEFORE: "This is some text outside parentheses and (inside parentheses)"
AFTER: "This is some text outside parentheses and [b][color="#808080"](inside parentheses)[/color][/b]"

FIND: "(?<string>\(.*\))" (without quotes)
REPLACE: "[b][color="#808080"]${string}[/color][/b]" (without quotes)

RegEX explanation:

(?<string>\(.*\)) match any text between parentheses, including them, and put the result in the named group "string".

"C:\Program Files\Find and replace\fnr.exe" --cl --dir "C:\test_dir" --fileMask "test.txt" --useRegEx --find "(?<string>\(.*\))" --replace "[b][color="#808080"]${string}[/color][/b]"

best regards

P.S: Eric, thank You for great tool!