A regular expression is a character string that consists of characters and metacharacters. You can use regular expressions to find character patterns in a text. For instance, regular expressions can help you find data that can be expressed in different formats, such as: Dates (e.g. mm/dd/yy, dd-mm-yyyy).
Tosca TBox supports regular expressions, which are used to compare whether the target attribute contains a string that matches the regular expression. The regular expression must be specified within double quotation marks.
Syntax:
{REGEX["regular expression"]}
A leading escape character is required in order to allow quotation marks to be used accordingly in the regular expression.
Metacharacters: Metacharacters are characters with a special meaning. You can use the following metacharacters in regular expressions.
[ ] -Finds every character in the square brackets. we can group several characters.
Ex -
[abc] matches a, b or c.
[a-z] defines the range of lowercase letters from a to z.
[^] - Finds every character that is not in the square brackets. We can group characters and use ranges.
Ex -
[^abc] matches every character except a, b or c.
[^a-z] matches every character except lowercase letters from a to z.
. - Matches any single character except for a line or paragraph break.
Ex - a.c matches abc, aTc, a$c, a c etc.
\d - Finds numeric characters.
Ex - \d\d\d matches any three digit number, e.g. 145, 238 etc.
\w - Finds alphanumeric characters.
Ex - \w matches a, b, c, d, e, f in abc def.
+ - Specifies that the preceding element appears one or more times.
Ex - ab+c matches abc, abbc, abbbc etc.
? - Specifies that the preceding element appears once or not at all.
Ex - ab?c matches ac or abc.
* - Specifies that the preceding element appears zero or more times.
Ex- ab*c matches ac, abc, abbc etc.
{n} - Specifies that the preceding element appears exactly n times.
Ex- ab{3}c matches abbbc.
{n,m} - Specifies that the preceding element can appear at least n times but not more than m times.
Ex- ab{2,4}c matches abbc, abbbc and abbbbc.
\b - Finds a word boundary.
Ex - \b\d\d\w+ matches a character string that begins with two numbers and continues with one or more alphanumeric characters, e.g. 12He(\t34aq.
( ) - Defines sub-expressions.
Ex - (\d\d\d\s)+ matches sequences of three-digit numbers, e.g. 123 456 789 012.
| - Finds either the preceding or following element.
Ex - (ab)|(cd) matches ab or cd.
\ - Specifies if the following character has a literal or special meaning.
Ex - \+ indicates the plus sign, + is a metacharacter.
\d is a metacharacter, d indicates the respective letter of the alphabet.
^ - Indicates the beginning of a line.
Ex - ^The matches any string that begins with The.
$ - Indicates the end of a line.
Ex - $end matches any string that ends with end.
\s - Finds whitespace characters.
Ex - \s* matches zero or more whitespace characters.
i - Uses case-insensitive matching.
Ex - (?i)test ignores upper-case and lower-case to find a match, e.g. test, Test, TEST, teST etc.