Combining Patterns Using Multiple Fields
This example shows how you can easily generate a regular expression to handle all kinds of (weird) identification codes by combining several fields into a regular expression. You can find this example as “Fields: multiple” in the RegexMagic library.
We want to create a regular expression that checks whether a particular string is a valid SKU code. SKU codes are used in inventory management to uniquely identify products. The SKU codes in this example are completely made up. It provides redundant human-readable information that makes it easier to prevent mistakes than an opaque ID that only consists of digits.
The format for the SKU codes in our example is 000000SUPPLIER/PRODUCTMMDDYY. A 6-digit unique product number, a supplier name, a slash, a product name (can contain digits), and 6 digits representing a date. The supplier and product names can each be between 1 and 16 characters in length.
- 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:
018719ACME/WMD001080165
028912MS/WIN95082495
032789JGSOFT/EPP6070106
- Set the subject scope to “line by line”. Our sample text has 3 lines, each with one SKU number. Because we want a regex that allows only one SKU code per string being validated, we need to tell RegexMagic to pretend that we have 3 samples (1 line each) rather than 1 sample with 3 lines.
- Click the button to add field .
- Set both “repeat this field” spinner controls to 6 to require this field to match 6 characters.
- In the “pattern to match field” drop-down list, select “basic characters”. We use this pattern instead of the “integer” or “numbers” pattern because we want to match exactly 6 digits.
- Tick the “digits” checkbox in the settings for the “basic characters” pattern.
- Click the Add Next Field button to add field .
- Set the “repeat this field” spinner controls for field to 1 and 16 in order to allow between 1 and 16 characters for this field.
- RegexMagic copied the pattern from field to field . All we need to do is to untick “digits” and to tick “uppercase letters” instead.
- On the Samples panel, select the slash after ACME, and click the Mark button. That’s all it takes to add field and make it match a slash.
- Click the Add Next Field button to add field .
- Set the “repeat this field” spinner controls for field to 1 and 16 in order to allow between 1 and 16 characters for this field.
- In the “pattern to match field” drop-down list, select “basic characters”.
- Tick the “digits” and “uppercase letters” checkboxes in the settings for the “basic characters” pattern.
- Click the Add Next Field button to add field .
- In the “pattern to match field” drop-down list, select “date and time”.
- Select “leading zeros required” in the “leading zeros” drop-down list.
- Enter mdy into the “allowed date and time formats” box.
- Set the “field validation mode” to “average”.
- Set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”. Our SKU code must begin and end with the subject string. There shouldn’t be any text before or after the SKU code.
- On the Regex panel, select “C# (.NET 2.0–8.0)” as your application, turn on free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
\A
[0-9]{6}
[A-Z]{1,16}
/
[0-9A-Z]{1,16}
(?:1[0-2]|0[1-9])(?:3[01]|[12][0-9]|0[1-9])[0-9]{2}
\z
Required options: Case sensitive; Free-spacing.
Unused options: Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.
- The Samples panel now shows the regular expression matches:
018719ACME/WMD001080165
028912MS/WIN95082495
032789JGSOFT/EPP6070106
Related Examples
Reference