i’m trying to find a way to match this problem but i’m LOST
i want it to match a math equation that will accept string if:
- “+-*/%” at anywhere in the string but not in the last character of the input string
- it accepts float and integers
- it doesn’t accepts letters just numbers and signs
any help ?
Thanks in advance !!
The regular expression you’re after looks something like:
Basically this means:
A number is defined as:
Now the leading optional minus is not strictly in your requirements so you can safely drop it but without it you can’t recognize:
which violates your condition of two operands together. To drop it just drop
-?from the expression (in both places) ie:It’s also not clear if you want to accept many operands or just one. If it’s just one then:
or
or a variant allowing spaces (
\s). You will probably also want to capture the groups using parentheses.You may want to alter that definition.
Also you could consider allowing spaces between numbers and operands.
An example of usage: