Use Regular Expressions

A regular expression is a string consisting of alphanumeric characters and special characters that is used in computing to match against other strings. In regular expressions some characters stand for themselves, while others have a special meaning.

Regular expressions can be used in Policy Modeling to ensure that the data entered by the user matches an expected format. You can also specify a custom error message to display when the user enters data that does not conform to the format. An example of where you might use a regular expression would be to check that a driver's license number contains the correct number of digits and alphabetical characters in the correct relative positions.

Regular expressions can only be used to validate input for text attributes.

Common regular expressions are given in the following table.

Tip: You can use AI chatbots to help you craft your regular expressions.

Table 1. Examples of regular expressions
Use Regular Expression
To check basic types of email addresses ^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$
To check the data entered follows a certain format: 2 letters 6 numbers 1 letter (for example, AB123456C) ^[A-Za-z]{2}[0-9]{6}[A-Za-z]$
To check the data is a phone number in the format: (NN) NNNN NNNN where N = number. This example will match with or without the 2 spaces separating the sections. ^(\(0[0-9])\) ?[1-9][0-9]{3} ?[0-9]{4}$
To validate a password to be between 4 and 8 digits long and to include at least one numeric digit. ^(?=.*\d).{4,8}$
To check that a text input does not exceed a certain number of characters (whitespace and non-whitespace characters), for example 40 characters. [\s\S]{0,40}
To validate that the number of words in a text input does not exceed a certain number. In this example, the value N is equal to the number of permitted words minus one, so if the permissible number of words is 8 then, N = 7. ^\s*(\S+(\s+\S+){0,N}\s*)?$
To check a National Insurance Number (NINO) ^[A-CEGHJ-PR-TWZ][A-CEGHJ-NPR-TWZ] ?\d{2} ?\d{2} ?\d{2} ?[ABCD]

Note: Intelligent Advisor uses the built in matching capabilities of Java regular expressions and JavaScript regular expressions. Be sure to only use regular expression syntax that is supported by both languages, and by all browsers. This is basically POSIX ERE regular expressions, with Perl/Tcl character classes. For more information, including examples on crafting regular expressions, see the Wikipedia article on Regular expressions.

To specify a regular expression for an attribute: 

  1. On the Data tab, double-click the attribute in the attribute list to open the Edit Attribute dialog box.
  2. Check that the attribute is a text attribute. If it is not, select Text from the drop-down list in the Value area of the Edit Attribute dialog box.
  3. Select the Input Validation button.
  4. In the drop-down window, enter the regular expression in the Regular Expression text field.
  5. (Optional) Enter a message in the Error Message text field, to be displayed to the user when the validation is triggered. If no message is provided, the default error message "Invalid Value" will be used.
  6. Click OK.