3.38.2. Using the ’matches’ operator
Prev | 3.38. Using simple matching and regular expressions for text verification | Next

### 3.38.2. Using the ’matches’ operator

Simple matching

- abc matches ”abc” and nothing else.

Wildcards

- . represents one instance of any character.

- .. represents two characters.

Repetition

- Instead of using . for each character, you can use symbols to indicate how many characters you are replacing with wildcards.

- ? matches the previous character or group 0 or 1 time(s). E.g. a? represents ”none or one a”.

- * matches the previous character or group 0 or more time(s). E.g. a* represents ”none or more a”.

- + matches the previous character or group 1 or more time(s). E.g. a+ represents ”one or more a”.

- You can also use curly brackets {} to specify the minimum and maximum number of repetitions, e.g. {4,7} looks for a minimum of four and a maximum of seven repetitions of the previous character.

- To specify that a character must be repeated a minimum of four times, use: {4,}

Combining wildcards with repetition

- You can specify a whole area of unknown text using . and one of the repetition methods.

- .* is an unlimited amount of any character, or none at all.

- .? is 0 or one of any character.

- .+ is an unlimited amount of any character, but the character must appear at least once.

| The regular expression is applied to your entire string. To search for a match within a string, wildcards need to be placed on either side. See the examples below for more information.

Ranges

- For each individual character, you can specify a range of things it is allowed to be.

- A range is specified using square brackets ([]) and a dash -.

- For example, you can specify that a particular character can be any capital letter: [A-Z].

| Note that there are no spaces between the ranges.

Alternatives

- Use a pipe (’|’) to specify alternatives.

- For example, [a|b].* will match a string that begins with a or b.

Escape character

- Backslash \ is used to negate the effect of the character following the backslash.

- The characters that are used to construct a regular expression need to be escaped if they are to be matched within a string.

- The characters are:

  `[ ] \ . | ? * + ( ) { } ^ $`

- Because backslash is already used as an escape symbol, you will need to use two backslashes to escape regular expression characters.

- For example, to check for a tree node:

  x/y/z/***

  where the slashes are a part of the node, your regular expression in the ITE would look like this:

  `x\/y\/z\/\\*\\*\\*`

  The backslashes before the ordinary slashes are an escape symbol to specify that the following sign is not a path separator. The extra backslash before the stars means that the second backslash is to be interpreted as a backslash in the regular expression, i.e. as an escape symbol.

- E.g. If you want to check for a star (*), then you have to enter \\*.

Verbatim

- You can avoid having to use multiple backslashes by putting the whole regular expression in single quotes:

- The example above for a tree node could be entered thus:

  'x/y/z/***'

Useful examples

- An empty field is represented by: `^$`

- A string that starts with a is represented by: a.*

- A string that ends in a is represented by: .*a

- A string that starts with a, ends in b and has unknown values (0 or more) in the middle is represented by: a.*b

- A string which contains a somewhere between other unknown characters (0 or more) is represented by: .*a.*

- A password which can only contain capital letters and which must be between six and eight letters is represented by: [A-Z]{6,8}.

- A password which can contain any alphanumerical values and which must be at least six characters is represented by: [A-Za-z0-9]{6,}.

- You can check that a string corresponds to a minimum and maximum length using:

  `'^.{'={MIN_COUNT}','={MAX_COUNT}'}$'`

- To check for a text which begins with a *, you must use the escape character: `\\*.*`

- To check that a specific string is not in a text, use:

  `^((?!STRING)[\s\S])*$`

For additional information about syntax and usage of regular expressions in general, please consult one of the many textbooks on the subject.