Skip to main content
edited title; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Simpler regex Regex for matching equationexpression that consists of single digit numbers and operators?

Source Link
Abe Miessler
  • 441
  • 1
  • 3
  • 11

Simpler regex for matching equation that consists of single digit numbers and operators?

I would like to create a regex that will validate that a string is an equation made up of a single digits and either the * or + operator and is no longer than 100 characters. So these would be valid:

1+2*3*8+0 9 9*9 

And these would not be valid:

1++1 12+1 2*25 1+ 47 + +1 11 

I came up with the regex below to accomplish this:

^(\d{1}[\+\*]{1}){0,99}\d$ 

Which appears to work, but I'm curious if there is a cleaner way to accomplish this. Any suggestions or is this about as clean as it gets?

It is saved here if you would like to play with it.