Skip to content Skip to sidebar Skip to footer

Regex For Calculator Display

I was wondering if it is possible to make a regex for a calculator input with these conditions, the display starts with a 0: the fist char can be an + or - sign (optional) if the

Solution 1:

Something like this would work:

^[+-]?\d*\.*\d+(?:[+*\/-][+-]?\d*\.*\d+)*$
  • ^ - start string anchor
  • [+-]? - start with an optional positive or negative sign
  • \d*\.*\d+ - optional digit, followed by optional decimal, followed by required digit. This allows us to account for whole numbers or decimals.
  • (?:[+*\/-][+-]?\d*\.*\d+)* - repeating optional pattern of previous two bullet points with a required preceding operator
  • $ - end string anchor

https://regex101.com/r/srl7vj/1

Post a Comment for "Regex For Calculator Display"