Your expression looks for an optional -
or +
followed by zero or more digits ([0-9]{0,}
). I think you probably wanted one or more (+
). Similarly, the ?
in ([-+*/]?)
makes the operator optional. You probably also want to capture the various parts.
It is fine when I have something one operator between two operand but when I do 9+9+9 which is more then two it is not working.
If you want to allow additional operators and digits following them, you have to allow the latter part to repeat.
Here's my rough take:
^s*([-+]?)(d+)(?:s*([-+*/])s*((?:s[-+])?d+)s*)+$
Live Example (But there's something not quite right happening with the capture groups if you have three or more terms.)
Changes I made:
Use d
rather than [0-9]
(d
stands for "digit")
Put the digits and the operators in capture groups.
Don't make the digits or operator optional.
Allow optional whitespace (s*
) in various places.
The (?: _________ )+
is what lets the part within those parens repeat.
Allow a -
or +
in front of the second group of digits, but only if preceded by a space after the operator.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…