0

I was wondering if it was possible to use regex to solve simple mathematical expressions with real numbers, and the operators +, -, *, / and ^.

For example the input would be a string like '3.5+4^2' (this could also be written as '+3.5+4^2') and the output 19.5. My idea was to have regex first recognise ^ as the procedure to perform first. So he would take 4^2 and return 16 so the expression would be '3.5+16'. Then it would recognise + and return 19.5.

Another input example would be -4+5.5*4/2 --> -4+22/2 --> -4+11 --> 7

  • You may use `eval` function. `print eval('3.5+4**2') > 19.5` – Valijon Dec 08 '15 at 12:14
  • 1
    I assume you just want to do this for fun. But the expressions aren't built from a regular grammar. You need a parser that supports context free grammars. See for example: http://stackoverflow.com/questions/559763/regular-vs-context-free-grammars – steinar Dec 08 '15 at 12:16
  • @Valijon `eval` is a terrible solution unless you *really* know what you’re doing. – bfontaine Dec 08 '15 at 12:18

4 Answers4

2

Regular Expressions describe patterns on Strings, they cannot analyse Integers/Floats. Unless there is a very obscure hack to make it work (which I think is unlikely).

You can validate if a given string is a valid mathematical expression, but you cannot evaluate mathematical expressions using regexes.

Matheus208
  • 1,289
  • 2
  • 13
  • 26
2

Regular expressions just match, they don't calculate. Therefore, it is perfectly fine to use regular expressions to match your string, akin to

>>> re.match(r'^(?P<n>[0-9]+)(?P<op>[-+\*/])(?P<rest>.+)$', '1+2+3').groupdict()
{'n': '1', 'op': '+', 'rest': '2+3'}

The actual calculation, however, is best performed in Python itself.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • but can I use regex to extract the real number before and after ^, for example, evaluate that and then replace it in the string, until I get down to one number? –  Dec 08 '15 at 12:41
  • Sure, using `re.sub`. Instead of replacing strings, you can also first parse the whole tree of expressions and then evaluate it. – phihag Dec 08 '15 at 12:58
  • I've been trying to work this out for the last hour and didn't really get anywhere. Any chance you could help me out? –  Dec 08 '15 at 14:12
  • Maybe irrelevant, but probably you can answer..... if I change order ie. -+ to +-, the expression doesn't parse (I'm using re.compile btw). Why so? – 0xc0de Feb 08 '16 at 07:31
  • 1
    @0xc0de If a `-` is at the very start or at the very end of a character class, it denotes a literal `-` character. Otherwise, it means the range of characters, as in `0-6`, which is shorthand for `0123456`. Just like that, `[*-/]` is shorthand for `[*+,./-]`. – phihag Feb 08 '16 at 08:42
0

You can do this by eval
^ can be replaced by ** to get the proper formula.

>>> eval('3.5+4^2'.replace('^', '**'))
19.5
>>> eval('-4+5.5*4/2')
7.0
Praveen
  • 8,945
  • 4
  • 31
  • 49
0

There is a famouse notation you should use for this, the Polish notation wiki page.

Regex won't help you a lot, because Regex use graphs whereas the interpretation of the calcul line you want would use Threes.

You may be interested in threes, childs, leaf, rotate... behaviors. This is very like a scool case, making this with the Polish notation, and with threes structure you will make this reading your line from left to right sequentially and without regex