Typing in Your Own Replacement Text
As shown in the “replacing text match by fields with text matched by other fields”, RegexMagic can generate a replacement text along with the regular expression. If you’re already familiar, you may find it quicker to enter the replacement text yourself. This example shows how RegexMagic can use a replacement text that you provide. You can find this example as “Action: replacement text” in the RegexMagic library.
For this example, we’ll continue with the regular expression created in the example about the “pattern used by another field” pattern. That regular expression matches three numbers delimited by commas.
Now we want to use this regex in a search-and-replace that swaps the first and third numbers. This is done by adding capturing groups to the regex and using backreferences to those groups in the replacement text.
- On the Library panel, load RegexMagic.rml if it isn’t loaded already.
- In the list of RegexMagic formulas in the library, select “Pattern: pattern used by another field”.
- Click the Use button to populate the Samples, Match, and Action panels with the settings from the RegexMagic formula we just loaded. The Match panel defines 5 fields. Fields , , and match the three numbers. Fields and match the commas that delimit them.
- On the Action panel, click the New button to add a capturing group. Set the group to capture field . We’ll use numbered backreferences in our replacement text, so don’t bother naming the group.
- Click the New button again to add a group to capture field .
- Click the New button one more time to capture field .
- Select “replace whole regex match” in the “action to take” drop-down list.
- Select “enter a replacement with backreferences” in the “how to replace” drop-down list.
- Set “replacement text flavor” to “%DEFAULTFLAVORREPLACE”.
- Now we can enter our replacement text:
${3},${2},${1}
- On the Regex panel, select “C# (.NET 2.0–8.0)” as your application, turn on free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get the regular expression below. If you compare this regex with the one we got in the example about the “pattern used by another field” pattern, you’ll see that three pairs of parentheses have been added. Those are our three capturing groups.
^
([+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3})
,
([+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3})
,
([+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3})
$
Required options: Free-spacing; ^$ match at line breaks.
Unused options: Case sensitive; Dot doesn’t match line breaks; Numbered capture.
The replacement text generated by RegexMagic is identical to the one we provided, because the replacement text flavor (.NET 1.1–8.0) we used on the Action panel is the same as the replacement text flavor used by the application (C# (.NET 2.0–8.0)) we selected on the Regex panel.
${3},${2},${1}
- If we select an application on the Regex panel that uses a different replacement text flavor then RegexMagic converts the replacement text from the flavor selected on the Action panel to the flavor used by the application selected on the Regex Panel. If we select Python on the Regex panel, for example, then RegexMagic generates the same regular expression, but a different replacement text:
\g<3>,\g<2>,\g<1>
Python uses a different syntax for backreferences in the replacement text. RegexMagic knows this and adjusts the replacement string on the Regex panel accordingly.
Related Examples
Reference