Regex and pattern matching.

Regular Expression, shortened as ‘regex’, is a pattern matching algorithm that is used in the flows to match a number or a group of numbers. This allows you to define a pattern for matching user input. This is useful when you want to validate inputs such as numeric codes, extension numbers, or specific digit patterns before proceeding with the call flow.

Taking example of the below regex, this regex matches a number, that can be presented in different variations. The number in this example is ‘35627790779’.

^(\\+356|356|00356|(?:))((27790779))$

The above regex matches with variations of numbers, ‘27790779’, ‘35627790779’, ‘0035627790779’ and ‘+35627790779’.

Full Breakdown

**^**
- Anchors the match to the start of the string.

**(\\+356|356|00356|(?:))**
- A group that matches one of the following Malta country code formats:
    - +356
    - 356
    - 00356
    - (?:) — this is a non-capturing group that matches nothing (effectively makes the prefix optional)

So, this part means: “Start with any of the Malta number prefixes or nothing.”

**((27790779))**
- Matches exactly the number 27790779.
- The extra parentheses make it a capturing group (nested capturing group, but unnecessary unless you have more than one number).

**$**
- Anchors the match to the end of the string.

A useful online tool can help you to check if the pattern is good, this site is **‘https://regex101.com/’**

image.png

As shown in the above image, all four variations of the same number result in a match, thus the pattern is correct and can be used.

You can also group numbers in the same regex, by using a ‘pipe / vertical bar’ ( ‘ | ’ ) between the numbers as shown in the below regex.

^(\\+356|356|00356|(?:))((27790779|27790770))$

This regex matches different variations for two numbers, ‘35627790779’ and ‘35627790770’.

image.png

Different conditions can be used to match specific numbers as shown below.

^(\\+356|356|00356|(?:))((2779077[1-9]{1}))$

This regex matches different variations of the numbers from ‘35627790771’ till ‘35627790779’

image.png