Regular expressions

Regular expressions are patterns that allow a bot to validate the format of user input — for example, to ensure that the input is a phone number rather than something else.

Regular expressions should be written in the trigger itself, not in the variables used for match!

When collecting data, users often enter something other than what they're asked for. Here's a typical flow for validating a phone number input. If the user doesn't enter a valid phone number, the bot will prompt them to try again.

The phone number input block has two outgoing connections: one without any trigger, and the other with a trigger that uses a regular expression to match a phone number.

^(\+)?((\d{2,3}) ?\d|\d)(([ -]?\d)|( ?(\d{2,3}) ?)){5,12}\d$

This flow uses two connections to handle phone number validation:

  1. Primary connection (valid number)

    • Trigger: A correctly formatted phone number.

    • Action: Adds the number to the application and proceeds.

  2. Fallback connection (invalid input)

    • Priority: Lower than the primary connection.

    • Trigger: Any input that is not a valid phone number.

    • Action: Informs the user of the error and redirects them to try again.

This ensures that the user only progresses after providing valid data, while receiving immediate feedback on any errors.

A list of useful regular expressions:

  • Numeric Input only digits starting with 1: ^[1-9]+[0-9]*$

  • Numeric Input only digits starting with 0: ^[0-9]+[0-9]*$

  • Credit card number: [0-9]{13,16}

  • General phone number: ^(\+)?((\d{2,3}) ?\d|\d)(([ -]?\d)|( ?(\d{2,3}) ?)){5,12}\d$

  • Letters and numbers (Latin): ^[a-zA-Z0-9]+$

  • Domain (e.g. abcd.com): ^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$

  • URL (e.g. abcd.com): (https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*

  • IPv4: ((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)

  • IPv6: ((^|:)([0-9a-fA-F]{0,4})){1,8}$

  • User name (with a limit of 2-20 characters, which can be letters and numbers, the first character need to be a letter): ^[a-zA-Z][a-zA-Z0-9-_\.]{1,20}$

  • Password (Lowercase and uppercase latin letters, numbers): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$

  • Strong password (uppercase, lowercase, numbers/special characters, min 8 characters): (?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

  • Date in YYYY-MM-DD format: [0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01]) UPD. Stricter date validation: (19|20)\d\d-((0[1-9]|1[012])-(0[1-9]|[12]\d)|(0[13-9]|1[012])-30|(0[13578]|1[02])-31)

  • Date in DD/MM/YYYY format: (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d

  • Date in DD.MM.YYYY format: (0[1-9]|[12][0-9]|3[01])[.](0[1-9]|1[012])[.](19|20)\d\d

  • Integer and floating-point numbers (dot as decimal separator) \-?\d+(\.\d{0,})?

  • UUID: ^[0-9A-Fa-f]{8}\-[0-9A-Fa-f]{4}\-[0-9A-Fa-f]{4}\-[0-9A-Fa-f]{4}\-[0-9A-Fa-f]{12}$

  • Latitude or Longitude: -?\d{1,3}\.\d+

  • UPD. E-mail: ^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$

  • UPD. URL ~^(?:(?:https?|ftp|telnet)://(?:[a-z0-9_-]{1,32}(?::[a-z0-9_-]{1,32})?@)?)?(?:(?:[a-z0-9-]{1,128}\.)+(?:ru|su|com|net|org|mil|edu|arpa|gov|biz|info|aero|inc|name|[a-z]{2})|(?!0)(?:(?!0[^.]|255)[0-9]{1,3}\.){3}(?!0|255)[0-9]{1,3})(?:/[a-z0-9.,_@%&?+=\~/-]*)?(?:#[^ '\"&]*)?$~i

  • UPD. Time in HH:MM:SS: format ^([0-1]\d|2[0-3])(:[0-5]\d){2}$

  • UPD. Mac-address: ([0-9a-fA-F]{2}([:-]|$)){6}$|([0-9a-fA-F]{4}([.]|$)){3}

There is a large number of regular expressions available. If you don’t find the one you need in this list, it’s a good idea to use a search engine.

You can conveniently test regular expressions on websites like: https://regex101.com/

Last updated