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.

  1. On the Library panel, load RegexMagic.rml if it isn’t loaded already.
  2. In the list of RegexMagic formulas in the library, select “Pattern: pattern used by another field”.
  3. 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 1, 3, and 5 match the three numbers. Fields 2 and 4 match the commas that delimit them.
  4. On the Action panel, click the New button to add a capturing group. Set the group to capture field 1. We’ll use numbered backreferences in our replacement text, so don’t bother naming the group.
  5. Click the New button again to add a group to capture field 3.
  6. Click the New button one more time to capture field 5.
  7. Select “replace whole regex match” in the “action to take” drop-down list.

  8. Select “enter a replacement with backreferences” in the “how to replace” drop-down list.
  9. Set “replacement text flavor” to “%DEFAULTFLAVORREPLACE”.
  10. Now we can enter our replacement text:
  11. On the Regex panel, select “C# (.NET 2.0–5.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. Number
    # 2. Literal text
    # 3. Same as field 1: Number
    # 4. Literal text
    # 5. Same as field 1: Number

    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–5.0) we used on the Action panel is the same as the replacement text flavor used by the application (C# (.NET 2.0–5.0)) we selected on the Regex panel.
  12. 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:
    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