Example of using regular expressions for string fields
A regular expression is a template that the handler of regular expressions compares with the entered text. The template includes single-character and multi-character literals, operators and structures. With regular expressions, one can determine if a regular expression template is a part of an input text.
You can find information about elements of the regular expression language here.
You can specify a regular expression or an error message when configuring a String-type property on the Advanced tab.
Regular expression – is a template, used to verify if the entered string is correct. If there is a discrepancy, in Web Application, the error message, specified in the Error message field will be displayed.
Error message – message text that will be displayed when entering a string that does not comply with the regular expression.
Each of the examples below includes:
‘^’ – the beginning of a string,
‘$’ – the end of a string,
‘?’ in ‘()?’ means, that everything within (), can repeat zero or one time. Unless you add this condition, the error message will appear if the field is not required or not filled in.
Example of the Full Name format
Example of checking details
Regular expression for checking passport series:
^([0-9]{2}\s{1}[0-9]{2})?$
Look into the expression:
- [0-9]{2} – allows entering any two numbers;
- \s{1} – allows entering one space;
- [0-9]{2} – allows entering any two numbers.
Example: 12 34
Regular expression for checking passport series:
^([0-9]{6})?$
Look into the expression:
- [0-9]{6} – allows entering any six numbers.
Example: 123456
Regular expression for checking a department code:
^([0-9]{3}[-]{1}[0-9]{3})?$
Look into the expression:
- [0-9]{3} – allows entering any three numbers;
- [-]{1} – allows entering one ‘-’;
- [0-9]{3} – allows entering any three numbers.
Example: 123-456
Regular expression for Individual Taxpayer Number:
^(([0-9]{12})|([0-9]{10}))?$
Look into the expression:
- [0-9]{12} – allows entering any twelve numbers;
- [0-9]{10} – allows entering any ten numbers;
- (a|b) – allows entering either the left part of the expression or the right part.
Example:
- 123456789012
- 1234567890
Regular expression for Primary State Registration Number:
^([0-9]{13})?$ – allows entering any thirteen numbers.
Regular expression for Industrial Enterprise Classification Code:
^([0-9]{9})?$ – allows entering any nine numbers.
Example of checking a phone number
Regular expression for checking a mobile phone number:
^([9]{1}[0-9]{9})?$
Look into the expression:
- [9]{1} – establishes that the first character is “9”;
- [0-9]{9} – allows entering nine numbers.
Example: 9123456789
Regular expression for checking a stationary phone number:
^([1-9]{1}[0-9]{9})?$
Look into the expression:
- [1-9]{1} – establishes that the first character is any number, except for 0;
- [0-9]{9} – allows entering nine numbers.
Example: 4951234567
Example of checking an email format
In this example, a regular expression is used to check if a correct email format was entered in the string.
A regular expression for checking email:
Look into the expression part by part:
- ([a-z0-9_-]+\.)* - allows entering:
- In [] you specify the range of characters: Latin alphabet from a to z in the lowercase, numbers and ‘_’ and ‘-’;
- The ‘+’ quantification after [] means, that at least one character from the specified range must be entered;
- In the expression ‘\.’, ‘\’ is an escape character, which indicates that ‘.’ is not a range of any characters, but a meta-character (i.e. a full stop is required at the end of the expression);
- ‘*’ in ‘()*’ means that everything within () can repeat zero or more times.
Example: ivan19.ivanovich.
- [a-z0-9_-]+@[a-z0-9-]+ - allows entering:
- Latin alphabet from a to z in the lowercase, numbers and ‘_’ and ‘-’;
- The ‘+’ quantification after [] means that at least one character from the specified range must be entered;
- ‘@’ character;
- Latin alphabet from a to z in the lowercase, numbers and ‘-’;
The ‘+’ quantification after [] means that at least one character from the specified range must be entered Example: smith2000@example_2000
- (\.[a-z0-9-]+)* - allows entering:
- In the expression ‘\.’, ‘\’ is an escape character, which indicates that ‘.’ is not a range of any characters, but a meta-character (i.e. a full stop is required at the end of the expression);
- Latin alphabet from a to z in the lowercase, numbers and ‘-’;
- The ‘+’ quantification after [] means that at least one character from the specified range must be entered;
- ‘*’ in ‘()*’ means that everything within () can repeat zero or more times.
Example: .example
- \.[a-z]{2,6} – allows entering:
- In the expression ‘\.’, ‘\’ is an escape character, which indicates that ‘.’ is not a range of any characters, but a meta-character (i.e. a full stop is required in the end of the expression);
- Latin alphabet letters;
- In the {} quantification, the permissible quantity of characters is specified, in this case – from 2 to 6.
Example: .com
This template allows entering the following email addresses:
- smith@example.com
- james_smith@example.com
- jamesroger.smith2000@example_2000.example.com
Other examples
Regular expression for checking a file name:
^[^\/:*?"<>|+]?$
Look into the expression:
- [^\/:*?"<>|+] – the ‘^’ character in the beginning of the range indicates negation (any symbols, except for the ones after ‘^’, are included in the range).
Example: Document1.docx
Regular expression for checking a file path:
^(([A-Z]:\\){1}([^\/:*?"<>|+]\\)*[^\/:*?"<>|+]{1})?$
Example:
- C:\Windows\System32\calc.exe
- Z:\test.txt