How to create regular expression only accept special formula?
The regular expression is a pattern containing various characters. We can use the regular expression to search whether a string contains a particular pattern.
Here, we will learn to create a regular expression to validate the various mathematical formulas. We will use the test() or match() method to check if particular mathematical formula matches with regular expression or not
Users can follow the syntax below to create regular expressions accepting special mathematical formulas.
let regex = /^\d+([-+]\d+)*$/g;
The above regex accepts only 10 – 13 + 12 + 23, like mathematical formulas.
Regular Expression Explanation
/ / – It represents the start and end of the regular expression.
^ – It represents the start of the formula string.
\d+ – It represents at least one or more digits at the start of the formula.
[-+] – It represents the ‘+’ and ‘-‘ operators in regular expression.
([-+]\d+)* – It represents that formula can contain digits followed by ‘+’ or ‘-‘ operator multiple times.
$ – It represents the end of the string.
g – It is an identifier to match all occurrences.
In the example below, we have created the regular expression which accepts the formula containing ‘+’ or ‘-‘ operators with digits.
Users can observe that the first formula matches the regex pattern in the output. The second formula is not matching with regex pattern as it contains the ‘*’ operator. Also, the third formula is the same as the first, but it contains the space between the operator and digit, so it doesn’t match with regular expressions.
A regular expression used in the example below
We have used the /^\d+(\s*[-+*/]\s*\d+)*$/g regular expression in the below example. Users can find the explanation of the used regular expression below.
^\d+ – It represents at least one digit at the start of the formula.
\s* – It represents zero or more white spaces.
(\s*[-+*/]\s*\d+)* – It represents that formula can contain space, operator, space, and digits multiple times in the same order.
In the example below, we have invoked the TestMultiplyFormula() function three times by passing various formulas as a parameter. We have used the test() method to check if the formula matches the regular expression pattern.
In the output, we can see that regular expression accepts the formulas with the ‘*’ and ‘/’ operators and white spaces.
This tutorial taught us to create a regular expression that accepts special mathematical formulas. In both examples, we have used the test() method to match the formula with regular expression. Also, we have used different regular expression patterns in both examples.
- Related Articles
- How to use special characters in Python Regular Expression?
- Regular Expression Special Variables in Perl
- How to escape any special character in Python regular expression?
- How to match only digits in Python using Regular Expression?
- Moving all special char to the end of the String using Java Regular Expression RegEx)
- MySQL regular expression to update a table with column values including string, numbers and special characters
- How to convert Regular expression to Finite Automata?
- How to match parentheses in Python regular expression?
- How to use wildcard in Python regular expression?
- How to use range in Python regular expression?
- How to use variables in Python regular expression?