Pattern: State or Province

“State or province” is one of the patterns that you can select on the Match panel. Use this pattern to make a field match the name or code of a state or province from a list of specific states or all states from specific countries.

You could use this pattern to check whether a valid US state name or abbreviation was entered into the state field on an order form. RegexMagic makes it easy to restrict the pattern to only those states that you can actually ship to. In this example, we’ll exclude APO addresses.

  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:
    Alabama
    AL
    Alberta
    AB
  3. Set the subject scope to “line by line”.
  4. On the Match panel, select Alabama in the sample text.
  5. Click the Mark button. RegexMagic automatically adds a field using the state pattern. The option “full names” is automatically selected along with “US States” in the list of countries and Alabama in the list of states.
  6. Select AL in the sample text.
  7. Click the 1 button. It’s important to use this button instead of the Mark button, because we want to provide an additional sample for the same field instead of adding a new field. RegexMagic automatically detects we also want state codes and ticks the “two-letter codes” checkbox.
  8. Right-click on the list of states and select Check All. This adds all US states to the pattern.
  9. At the end of the list, uncheck the three Armed Forces “states”.



  10. On the Match panel, set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”.
  11. On the Regex panel, select “C# (.NET 2.0–8.0)” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \A(?:Ala(?:(?:bam|sk)a)|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|District of Columbia|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Miss(?:(?:issipp|our)i)|Montana|Nebraska|Nevada|New (?:Hampshire|Jersey|Mexico|York)|North (?:(?:Carolin|Dakot)a)|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South (?:(?:Carolin|Dakot)a)|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming|A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])\z

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

  12. The Samples panel now highlights the US states, but not states or provinces from other countries:
    Alabama
    AL
    Alberta
    AB

States or Provinces from Other Countries

If the RegexMagic pattern for states and provinces does not support your country, you can use the “list of literal text” pattern. Type or paste in the list of states or provinces for your country and the pattern will match any one of them.

Reference