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.

  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:
    018719ACME/WMD001080165
    028912MS/WIN95082495
    032789JGSOFT/EPP6070106
  3. 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.
  4. Click the Add First Field button to add field 1.
  5. Set both “repeat this field” spinner controls to 6 to require this field to match 6 characters.
  6. 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.
  7. Tick the “digits” checkbox in the settings for the “basic characters” pattern.
  8. Click the Add Next Field button Add Next Field to add field 2.
  9. Set the “repeat this field” spinner controls for field 2 to 1 and 16 in order to allow between 1 and 16 characters for this field.
  10. RegexMagic copied the pattern from field 1 to field 2. All we need to do is to untick “digits” and to tick “uppercase letters” instead.
  11. On the Samples panel, select the slash after ACME, and click the Mark button. That’s all it takes to add field 3 and make it match a slash.
  12. Click the Add Next Field button Add Next Field to add field 4.
  13. Set the “repeat this field” spinner controls for field 4 to 1 and 16 in order to allow between 1 and 16 characters for this field.
  14. In the “pattern to match field” drop-down list, select “basic characters”.
  15. Tick the “digits” and “uppercase letters” checkboxes in the settings for the “basic characters” pattern.
  16. Click the Add Next Field button Add Next Field to add field 5.
  17. In the “pattern to match field” drop-down list, select “date and time”.
  18. Select “leading zeros required” in the “leading zeros” drop-down list.
  19. Enter mdy into the “allowed date and time formats” box.
  20. Set the “field validation mode” to “average”.



  21. 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.
  22. 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:
    \A
    # 1. Basic characters
    [0-9]{6}
    # 2. Basic characters
    [A-Z]{1,16}
    # 3. Literal text
    /
    # 4. Basic characters
    [0-9A-Z]{1,16}
    # 5. Date and time
    # mdy
    (?: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.

  23. The Samples panel now shows the regular expression matches:
    018719ACME/WMD001080165
    028912MS/WIN95082495
    032789JGSOFT/EPP6070106

Related Examples

Reference