0

When you type in google search something like this:

5.5 + 0.5  * 56

google calculate it

5.5 + (0.5 * 56) = 33.5

This is really awesome regex pattern but i'm not very good with regex :(

Ben
  • 1,906
  • 10
  • 31
  • 47

3 Answers3

1

What Google does is parse the mathematical expression, it doesn't use matching patterns.

You need a recursive descent parser for this: Equation (expression) parser with precedence?

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

As far as i know, regex can't do calculations like this...and google doesn't develop his search engine only by using regular expressions. To do this kind of jobs you need to implement yourself an expression calculator. Years ago i've developed a simple one and it worked very well. On the net there are a huge number of example to do that...hope it heelps!

GiveEmTheBoot
  • 534
  • 9
  • 24
0

Regular expressions are for searching and manipulating texts based on patterns. Again not every pattern it can recognize. It only capable of understanding patterns based on regular language (Type-3 grammars in Chomsky hierarchy).

What you are showing here is done by mathematical expression parsers. Just google it and you'll find plenty of them freely available. If you have a JavaScript console in the browser you are using, just use eval to parse the expression.

var expression = "5.5 + 0.5  * 56";
eval( expression );
// 33.5
soimon
  • 2,400
  • 1
  • 15
  • 16
intellidiot
  • 11,108
  • 4
  • 34
  • 41