What is a "Regular Expression"?
In simple terms, a regular expression is a pattern by which a Telegram bot accepts data entered by the user; if the data does not match the pattern (regular expression), the question in the feedback module from the client remains unanswered — the bot does not accept the data.
There are ready-made templates for validating entered data:
- Checking the correctness of entering a phone number
- Checking the correctness of entering an email address
- Checking links
In other cases, you need to create the regular expression yourself; let’s figure out how to do that!
Basics of Regular Expression Construction
1. A regular expression or Reg Exp. consists of various symbols, which in turn are parameters for configuring the expression.
A regular expression is ALWAYS placed in /^regular expression$/, any other format will not work!
Let’s look at the basic Reg Exp metacharacters:
- ^ - start of the string, for example, ^BOT - this expression will match strings that start with the word "BOT"
- $ - end of the string, for example BOT$ - this expression will match strings that end with the word "BOT"
- . - any character, except a newline
- * - zero or more repetitions of the previous character
- + - one or more repetitions of the previous character
- ? - zero or one repetition of the previous character
- \d - matches any digit from 0 to 9
- | - otherwise the conjunction "OR", this expression - BOT|CODE - will match strings that contain the words "BOT" or "CODE"
2. If you need to check the correctness of entering a single word, you can use a regular expression consisting of simple familiar symbols, for example:
- We want the client in the feedback form to type the word "Turtle", let’s imagine this word is a password, after which the feedback module will continue working according to the specified scenario.
- To check the word entry, we insert the following regular expression: /^Черепашка$/, after entering the code word - the bot will successfully accept it and continue working.
3. In the screenshot in the previous block you could see the regular expression /^\d*?((0)|(2)|(4)|(6)|(8))$/, it corresponds to checking whether an even number is entered, i.e. when an odd number is entered - the bot will report an error.
In this Reg Exp. we already see familiar symbols, where:
- The first entered digit \d must correspond to any digit from 0 to 9
- The symbol * means zero or more repetitions
- The next metacharacter - ?, meaning zero or one repetition of the previous character to avoid errors if, for example, we want to enter the number 88.
Probably you have a question "Why then do * and ? appear in the same expression" - the answer is simple, for the case of entering multi-digit numbers, whether it is 88 or 888
- We also see the symbol | - meaning that after the first and subsequent digits there is 0 OR 2 OR 4 OR 6 OR 8 - these are even digits, which we need!
- Why are the digits enclosed in parentheses? From the basic rules of mathematics: an even or odd number is determined by the last digit of the number, these digits and the metacharacter | relate to the end of the string - the metacharacter $
So we have figured out what regular expressions are and how to construct them correctly; this article does not present all metacharacters, and to fully study regular expressions you will need to spend some time on it.
free of charge and without programming knowledge BOT-T.COM