Pattern: National ID

“National ID” is one of the patterns that you can select on the Match panel. Use it to make a field match a personal identification code used in a specific country such as social security numbers, identity numbers, citizen numbers, tax numbers, license plate numbers, etc.

This example shows how you can use the “national ID” pattern to look for US social security numbers. You can find this example as “Pattern: national ID” in the RegexMagic library.

  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 that has a block of valid and a block of invalid US social security numbers:
    Valid:
    123-12-1234
    078-05-1120
    
    Invalid:
    123121234
    1234-12-1234
    000-00-0000
  3. On the Match panel, click the Add First Field button to add field 1.
  4. In the “pattern to match field” drop-down list, select “national ID”.



  5. In the “kind of ID” drop-down list, select “US social security number”.
  6. Set the “field validation mode” to “strict”, to make sure the “national ID” pattern generates a regex that excludes invalid social security numbers such as those beginning with 000 or 666.
  7. On the Regex panel, select “C# (.NET 2.0–4.8 & .NET Core 1.0–3.0)” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \b(?!000|666)[012345678][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}\b

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

  8. The Samples panel now highlights the valid social security numbers matched by our regular expression:
    Valid:
    123-12-1234
    078-05-1120
    
    Invalid:
    123121234
    1234-12-1234
    000-00-0000

Reference