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.
- Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
- On the Samples panel, paste in one new sample:
- On the Match panel, set “begin regex match at” to “start of line”, and set “end regex match at” to “end of line”.
- Again on the Samples panel, select the first number, and click the Mark button. This marks the first number as field .
- Select the first comma, and click the Mark button to mark it as field .
- Select the second number, and mark it as field .
- Mark the second comma as field .
- Mark the third number as field . The whole sample has been marked now:
- On the Match panel, use the “select field” drop-down list to select field . 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.
- Set the minimum and maximum values of the integer part to -999 and 999.
- 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:
- Use the “select field” drop-down list again to select field . RegexMagic detected the “number” pattern for this field too, but with different options.
- In the “pattern to match field” drop-down list, select “pattern used by another field”.
- Use the “select field” drop-down list again to select field .
- In the “pattern to match field” drop-down list, select “pattern used by another field” once more.
- Set the “field validation mode” to “strict”, to make sure the number pattern uses all the settings we made.
- 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 this regular expression:
Required options: Free-spacing; ^$ match at line breaks.
Unused options: Case sensitive; Dot doesn’t match line breaks; Numbered capture.
- The Samples panel now confirms our regular expression matches the 3 comma-delimited numbers: