Pattern: Pattern used by another field

“Pattern used by another field” is one of the patterns that you can select on the Match panel. If two or more fields need to match the same kind of pattern, but not match exactly the same text, you need to specify the pattern only for the first field that needs it. For the other fields, you can select “pattern used by another field” and reference the first field.

For this example, we’ll try to match a comma-delimited list of 3 numbers. The 3 numbers can be different, but they must all 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,-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 whole sample has been marked now:
    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, but with different options.
  13. In the “pattern to match field” drop-down list, select “pattern used 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 “pattern used 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–7.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. Number
    [+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3}
    # 2. Literal text
    ,
    # 3. Same as field 1: Number
    [+-]?(?:[1-9][0-9]{1,2}|[0-9])\.[0-9]{3}
    # 4. Literal text
    ,
    # 5. Same as field 1: Number
    [+-]?(?:[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.

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

Related Examples

Reference