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.

number” in the “field to replace” drop-down list.
number” in the “field to replace with” drop-down list. This tells RegexMagic we want to replace the text matched by field
with the text matched by field
.
number” in the “field to replace” drop-down list.
number” in the “field to replace with” drop-down list. This tells RegexMagic we want to replace the text matched by field
with the text matched by field
.
, the text matched by fields
through
, and the text matched by field
.
^ # 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.
, the second reinserts fields
through
, and the last one puts field
at the end of the replacement.
${field5}${fields2to4}${field1}
^(?<field1>[+-]?(?:[1-9][0-9]{1,2}|[0-9])\.\d{3})(?<fields2to4>,[+-]?(?:[1-9][0-9]{1,2}|[0-9])\.\d{3},)(?<field5>[+-]?(?:[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.
$<field5>$<fields2to4>$<field1>