Regular expression to match digits and basic math operators

I need a regular expression that will match 0-9, (,),+,-,* and /.

It looks like you might be trying to match numeric expressions like 5+7-3.

This should match them :

([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+)

Regex to match digits and math operators, How about this, it works for all your cases, under perl, and won't match letters, except the strings "cos, sin, tan" and Pi. ((?:[-\d)(+/*]+)?(?:(?:cos|sin|tan)[(](? The Match (String, Int32, Int32) method returns the first substring that matches a regular expression pattern in a portion of an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language - Quick Reference.

The accepted answer can't handle a lot of basic cases. This should do the job:

^([-+]? ?(\d+|\(\g<1>\))( ?[-+*\/] ?\g<1>)?)$

Explaination:

We want to match the entire string:

^...$

Expressions can have a sign:

[-+]? ?

An expression consists of multiple digits or another valid expression, surrounded by brackets:

(\d+|\(\g<1>\))

A valid expression can be followed by an operation and another valid expression and is still a valid expression:

( ?[-+*\/] ?\g<1>)?

Regular Expressions, Certain characters have special purposes in regular expressions. These are called For example, parentheses are used for grouping, just as they are in arithmetic. If you want to use a Given a string, try to find a substring that matches the pattern. Given a string [+\-*/], matches any one of the usual arithmetical operators. A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.(Wikipedia). Regular expressions are a generalized way to match patterns with sequences of characters.

[\d\(\)\+\-\*\/\.]

Regex - Common Operators, Most operators have more than one representation as characters. The result is a regular expression that will match a string if a matches its first part and b Regex treats this sequence as a unit, just as mathematics and programming� Regular Expressions for Data Science (PDF) Mar 25, 2020 · This page describes the regular expression grammar that is used when std::basic_regex is constructed with syntax_option_type set to ECMAScript (the default). 3f instanceof Integer // 1 // the base is an int, and the The following Regular Expression will match both lines because there is

I think you are looking for character classes

[0-9()+\-*/.]

This should match a word that contains any number from 0 to 9 or ( ,),+,- ,/ or *

Gcal 4.1: Regexp Operators, You can combine regular expressions with the following characters, called regular expression operators, or metacharacters, to increase the power and versatility of regular matches any one of the characters ' M ', ' V ', or ' X ' in a string. Ranges of Parentheses are used for grouping in regular expressions as in arithmetic. A regular expression is a pattern that provides a flexible and concise mean to match the string of text. A regular expression is also referred to as regex or regexp. A regular expression can be either simple or complex, depending on the pattern you want to match.

If you need a regex to match an arithmetic expression like this: 3+2-24*2/2-1 you can try this:

String reg1="([0-9]+[\\+\\-\\*\\/]{1}[0-9]+)+([\\+\\-\\*\\/]{1}[0-9]+)*";

You can add the bracket where do you want if you'll edit this regex.

Regular expression, A regular expression is a sequence of characters that define a search pattern. Usually such For example, in the regex a. , a is a literal character which matches just 'a', while '. regular languages using his mathematical notation called regular events. In the POSIX standard, Basic Regular Syntax (BRE) requires that the� Regular expression to match digits and basic math operators. Related. 587. 4322. Regular expression to match a line that doesn't contain a word. 1374.

Regular Expressions, Regular Expressions (abbreviated regex) are the most useful tools in string processing. If you are fond of the search and replace tool in your favorite text� You can use range quantifier {min,max} to specify minimum of 1 digit and maximum of 6 digits as: ^[0-9]{1,6}$ Explanation: ^ : Start anchor [0-9] : Character class to match one of the 10 digits {1,6} : Range quantifier. Minimum 1 repetition and maximum 6. $ : End anchor Why did your regex not work ? You were almost close on the regex:

Regular expressions, Regular expressions are patterns used to match character combinations in strings. A regular expression pattern is composed of simple characters, such as /abc/ , or a combination of simple and character properties, for example, upper- and lower-case letters, math symbols, and punctuation. Expressions & operators. Match expression expr1 or expression expr2. If there is a match with expr1 , then expr2 is ignored. '(let|tel)\w+' matches words that start with let or tel .

[PDF] Regular Expressions: The Complete Tutorial, Matching Floating Point Numbers with a Regular Expression .72. 3. mathematical theory on which they are based. The most basic regular expression consists of a single literal character, e.g.: �a�. The alternation operator has the lowest precedence of all regex operators. Use the string-concatenation operator & to combine your own text strings with members of the Match enum: For example, the pattern "A" & MultipleDigits will match the letter "A" followed by one or more digits.

Comments
  • using preg? ereg? What language? What system?
  • This answer does not incorporate the brackets mentioned in the question. What if you have an operation (2+3)*(5/(7-1))?
  • In addition, that incorrectly says 25*(53+5 is valid and 2*(3) is not. If you wanna try and find more: jsfiddle.net/a7eg9a78
  • how use \g<1> in javascript? I understand that this is only with PCRE regex? regex101.com/r/HdBU3E/2
  • @hizmarck indeed, this works only for PCRE regex engines.
  • I dont think you need to escape (,),-,+ etc in a character class . It depends on the lanaguage doesnt it ?
  • @Jass, in Perl at least you are right. You do need to take care however that you either escape / or write the regexp like m~[\d()+*/.-]~.
  • The escape syntax depends on the language you're using.
  • Yes, we escape the / there because the language requires it so that it can identify the / and pass it on to the regex engine instead of eating it. Like you say the usage of m~~ is a workaround.. :)
  • +-* is an invalid range, put the '-' at the end and it should work
  • I think the - should be the first character, like [-0-9()+*/], no?
  • Yes I think the solution is character classes, but in response to inshallah, shouldn't just escaping the '-' be sufficient? My understanding is that depending on the system (which has atm not been specified by OP), characters such as that can have special meanings, in fact so can *, and the brackets. I think OP would do best to read whatever documentation there is for his particular regex system.
  • @Victor, putting the - first works as well :-), never knew.
  • I marked you answer as useful because it helped me in my work. All above answer did not help me much. Thanks.
  • Although this code might solve the problem, a good answer should also explain what it does and how it helps.