Regular expressions

If you choose the Regex option in the Form designer, you can program the structure for the data that appears.

Only the Text capture type offers full regular expression capabilities. The other capture types offer other, simple restrictions to control the data input.

The table below illustrates the more common characters that can be used in the Regex field and gives you some examples of the matching performed.

Field

What does it do?

Example

Matches

^

Matches beginning of line

This marks the start of your regex entry

 

$

Matches end of line

This marks the end of your regex entry

 

.

Matches any character

^a.c$

abc, a2c, axc

|

OR character

^abc|xyz$

abc or xyz

[…]

Matches anything contained in brackets

^[A-E]$

Can only use uppercase A to E

[^…]

Matches anything not contained in brackets

^[^A-E]$

Cannot use uppercase A to E

{x}

The exact 'x' amount of times to match

^[A-E]{2}$

Can only use uppercase A to E and must contain at least 2 characters

{x,y}

Match between 'x' and 'y' times

^[A-E]{2,4}$

Can only use uppercase A to E and must contain at least 2 characters and no more than 4

{x,}

Match 'x' amount of times or more

^[A-E]{2,}$

Can only use uppercase A to E and must contain at least 2 characters but no limit on total number

+

Matches character before + one or more times

^ab+c$

Matches abc, or abbbc

?

Matches the character before the ? zero or one times

^ab?c$

Matches ac, or abc

!

Switches the matching of a character to be not allowed

^(?!000)[0-8][0-9]{2}$

Three digits between 001 to 899 allowed, but specifically 000 is not allowed

Example: National Insurance Number

^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$

Here is an explanation of this regular expression:

  • ^ – the beginning of string
  • [A-CEGHJ-PR-TW-Z]{1} – matches the first letter, it cannot be D, F, I, Q, U or V
  • [A-CEGHJ-NPR-TW-Z]{1} – matches the second letter, it cannot be D, F, I, O, Q, U or V
  • [0-9]{6} – must be six digits
  • [A-D]{1} – matches last letter which can only be A, B, C or D
  • $ – the end of the string

This is what a valid National Insurance Number looks like.

This is what an invalid national insurance number looks like.

Example Social Security Number

^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$

Here is an explanation of this regular expression:

  • ^ – the beginning of string
  • (?!000|666) – does not allow either 000 or 666 to be entered
  • [0-8][0-9]{2} – matches the first digit between 0 and 8, and the next two digits between 00 and 99
  • - – the user has to type a hyphen as a separator
  • (?!00)[0-9]{2} – matches two digits between 01 and 99, specifically stopping 00 from being entered
  • - – the user has to type a hyphen as a separator
  • (?!0000)[0-9]{4} – matches four digits between 0001 and 9999, specifically stopping 0000 from being entered
  • $ – the end of the string

This is what a valid Social Security Number looks like.

This is what an invalid Social Security Number looks like.