Evaluating a formula entered in a text box.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
sithik_frns
Forum Newbie
Posts: 1
Joined: Mon Jul 14, 2008 12:30 am

Evaluating a formula entered in a text box.

Post by sithik_frns »

Hi buddies,

I want to evaluate a formula (with variable names and limited operators eg. (a+b)-c )entered in a textbox using JavaScript or PHP. Can anyone here assist me?

Thanks in Advance

-: Sithik :-
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Evaluating a formula entered in a text box.

Post by prometheuzz »

sithik_frns wrote:Hi buddies,

I want to evaluate a formula (with variable names and limited operators eg. (a+b)-c )entered in a textbox using JavaScript or PHP. Can anyone here assist me?

Thanks in Advance

-: Sithik :-
You'll have to ask a more specific question than that.
Note that you have now posted in the regex forum. People often make the mistake of trying to write a mathematical expression parser with regex: this is really not the way to go. Regex is not the right tool.
So, I highly recommend you post your question in a more general part of this forum and ask a more specific question.

Best of luck.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Evaluating a formula entered in a text box.

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Evaluating a formula entered in a text box.

Post by Ollie Saunders »

People often make the mistake of trying to write a mathematical expression parser with regex: this is really not the way to go. Regex is not the right tool.
Regex can definitely be useful but it's but it's only a tiny part of the solution.

sithik_frns, this is actually a pretty common thing so try searching for some existing code/library and articles. You'll want to search for "expression evaluation"

If you want to roll you own solution for fun or other reasons start with some kind of language definition - what is it going to be capable of? and a bunch of examples - this code should give this answer. Then write down the process your brain goes through in order to compute those expressions - that is what you'll have to describe to the computer.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Evaluating a formula entered in a text box.

Post by prometheuzz »

Ollie Saunders wrote:
People often make the mistake of trying to write a mathematical expression parser with regex: this is really not the way to go. Regex is not the right tool.
Regex can definitely be useful but it's but it's only a tiny part of the solution.
...
True: as a part of the lexer, regex can be useful but, as you already mentioned, that is just a small part.
Post Reply