Pattern: Text matched by another field

“Text matched by another field” is one of the patterns that you can select on the Match panel. This pattern requires a field to match the exact same text that was matched by a previous field in the regular expression.

For this example, we’ll try to match a comma-delimited list of 3 numbers. The 3 numbers must be exactly the same. They can range from -999.999 to +999.999. The decimal digits are mandatory, the sign is optional.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    12.374,12.374,12.374
    -17.872,-17.872,-17.872
    519.878,519.878,519.878
    12.374,-17.872,519.878
  3. On the Match panel, set “begin regex match at” to “start of line”, and set “end regex match at” to “end of line”.
  4. Again on the Samples panel, select the first number, and click the Mark button. This marks the first number as field 1.
  5. Select the first comma, and click the Mark button to mark it as field 2.
  6. Select the second number, and mark it as field 3.
  7. Mark the second comma as field 4.
  8. Mark the third number as field 5. The first sample has been marked now:
    12.374,12.374,12.374
    -17.872,-17.872,-17.872
    519.878,519.878,519.878
    12.374,-17.872,519.878
  9. On the Match panel, use the “select field” drop-down list to select field 1. RegexMagic has correctly detected the “number” pattern for this field. But the settings for the pattern aren’t the ones we want. One sample is not enough for RegexMagic to extrapolate from.
  10. Set the minimum and maximum values of the integer part to -999 and 999.
  11. Tick “limit integer part”, “allow plus sign”, “allow minus sign”, and “require integer part”. Clear all the other checkboxes. The pattern should now look like this:



  12. Use the “select field” drop-down list again to select field 3. RegexMagic detected the “number” pattern for this field too, which isn’t what we want.
  13. In the “pattern to match field” drop-down list, select “text matched by another field”.



  14. Use the “select field” drop-down list again to select field 5.
  15. In the “pattern to match field” drop-down list, select “text matched by another field” once more.
  16. Set the “field validation mode” to “strict”, to make sure the number pattern uses all the settings we made.
  17. 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 this regular expression:
    ^
    # 1. field1: Number
    (?<field1>[+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3})
    # 2. Literal text
    ,
    # 3. Same text as field 1
    \k<field1>
    # 4. Literal text
    ,
    # 5. Same text as field 1
    \k<field1>
    $

    Required options: Case sensitive; Free-spacing; ^$ match at line breaks.
    Unused options: Dot doesn’t match line breaks; Numbered capture.

  18. The Samples panel now confirms our regular expression matches the 3 comma-delimited numbers, but only if the 3 numbers are the same:
    12.374,12.374,12.374
    -17.872,-17.872,-17.872
    519.878,519.878,519.878
    12.374,-17.872,519.878

Related Examples

Reference