Replacing Text Match By Fields with Text Matched by Other Fields

This example demonstrates how you can prepare a search-and-replace action with RegexMagic that swaps different parts of the regular expression match with other parts of the regular expression match. You can find this example as “Action: replace fields” 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. With RegexMagic, you only need to tell which fields you want to replace. RegexMagic automatically adds the necessary capturing groups and automatically generates 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.
  4. On the Action panel, select “replace all fields” in the “action to take” drop-down list.



  5. Select “1 number” in the “field to replace” drop-down list.
  6. Select “replace with another field” in the “how to replace” drop-down list.
  7. Select “5 number” in the “field to replace with” drop-down list. This tells RegexMagic we want to replace the text matched by field 1 with the text matched by field 5.
  8. Select “5 number” in the “field to replace” drop-down list.
  9. Select “replace with another field” in the “how to replace” drop-down list.
  10. Select “1 number” in the “field to replace with” drop-down list. This tells RegexMagic we want to replace the text matched by field 5 with the text matched by field 1.
  11. On the Regex panel, select “C# (.NET 2.0–4.8 & .NET Core 1.0–3.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. Notice the difference between this regex and the one we got in the example about the “pattern used by another field” pattern. Three named capturing groups have been added to capture the text matched by field 1, the text matched by fields 2 through 4, and the text matched by field 5.
    ^
    # 1. field1: Number
    (?<field1>[+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3})
    (?<fields2to4>
    # 2. Literal text
    ,
    # 3. Same as field 1: Number
    [+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3}
    # 4. Literal text
    ,
    )
    # 5. field5: Same as field 1: Number
    (?<field5>[+-]?(?:[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.

    We also get a replacement text that consists of three backreferences. The first backreference reinserts the text matched by field 5, the second reinserts fields 2 through 4, and the last one puts field 1 at the end of the replacement.
    ${field5}${fields2to4}${field1}
  12. If you select the JavaScript regex flavor, which does not support named groups, you’ll see that RegexMagic can use numbered capturing groups just as well.
    ^([+-]?(?:[1-9][0-9]{1,2}|[0-9])\.\d{3})(,[+-]?(?:[1-9][0-9]{1,2}|[0-9])\.\d{3},)([+-]?(?:[1-9][0-9]{1,2}|[0-9])\.\d{3})$

    Required options: ^$ match at line breaks.
    Unused options: Case sensitive; Dot doesn’t match line breaks.

    The numbers in the replacement text refer to the capturing groups. In a regular expression, unnamed groups are always numbered from left to right starting at one.
    $3$2$1

Related Examples

Reference