regex to match at least one symbol and at least 2 numbers

This will match your tests.

[0-9]+    # first number
s*       # any whitespace
[+*/-] # operand
s*       # any whitespace
[0-9]+    # second number

Add s to character class

  • s matches spaces.

  • The negated character class [^ ] ensures anything other than @ or space is matched

Regex Example


That should do it.

EDIT: But since some agree the escaping is too much, here ya go:


Im not sure if you want to match more than one, but if that is the case then remove "^" from the beginning and "$" from the end and also make it globally search like this:


Hope this helps

It's a bit difficult to review the regex without the sample input but couple of observations:

  • [0-9] can be replaced with d (since, you're already using it at the end)

  • [D] is exactly the same as D. It's a character class itself and unless you have some more characters to include it'ss fine without being enclosed in [].

  • (?:,) should simply be , because you neither want to capture it nor it has any quantifiers.

  • ,[W_] Here it seems you want to use the character class but the would escape the first [. If you actually need a literal there; you need to escape it as \ since it's a special character.


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © All Rights Reserved .