Regular Expression help!!! - Math

  • Thread starter Thread starter cbmeeks
  • Start date Start date
C

cbmeeks

I have a project that requires me to validate the syntax of basic math
formulas.

Something like: ( ( A * B ) / C) - 10

I need to just make sure that parenthesis are correct.

For example, you cant have a formula like: ) ( A * B ( / C ( - 10

Checking for equal number of ( as ) is easy.

Thanks!

cbmeeks
http://www.codershangout.com
 
This isn't much help but the best way it seems to solve your problem is
to parse through the entire string and when it comes accross a '(' or
')' it checks the rest of the varible to see if the next parenthesis is
open or closing. Once it accomplishes this, it starts checking the rest
of the string for a '(' or a ')'. If it ever comes accross a ')'
prematurely, you know the user is wrong.

There's probley a more efficent way to do it. That's just off the top
of my head.
 
I have a project that requires me to validate the syntax of basic math
formulas.

Something like: ( ( A * B ) / C) - 10

I need to just make sure that parenthesis are correct.

For example, you cant have a formula like: ) ( A * B ( / C ( - 10

Checking for equal number of ( as ) is easy.

Thanks!

cbmeeks
http://www.codershangout.com

No need for a regular expression if you are just checking parentheses.
Start a count at 0. Every time you hit '(' increment the count.
Every time you hit ')' decrement the count. If the count goes
negative then you have a ')' before its corresponding '('. If the
count is not zero at the end of the expression then you have a '('
without a corresponding ')'. If the count is zero at the end of the
expression then the parentheses are well formed.

rossum
 
Thanks! That is an excellent idea.

However, the following is returning OK:

( ) ) (

because it is zero.

I think after I pass the first check I will go through and do the other
check mentioned earler

cbmeeks
http://www.codershangout.com
 
cbmeeks said:
Thanks! That is an excellent idea.

However, the following is returning OK:

( ) ) (

because it is zero.

I think after I pass the first check I will go through and do the other
check mentioned earler

cbmeeks
http://www.codershangout.com

Add the condition that the "count" must never be less than zero as it
scans. Your example of ( ) ) ( will be negative -1 before it hits the
end, specifically just after the second ')' character.
 
Add the condition that the "count" must never be less than zero as it
scans. Your example of ( ) ) ( will be negative -1 before it hits the
end, specifically just after the second ')' character.

Erm, "your count will be negative 1" not "your count will be negative
-1."
 
In case you're still curious:

"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$"

Example Code:

public class App1
{

[STAThread]
static void Main()
{
string pattern =
@"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$";
string[] inputs = {"( ( A * B ) / C) - 10 ", " ) ( A * B ( / C ( -
10", "( ( A * B / C) - 10 ", "()", "(()", "(()))"};

foreach(string input in inputs)
{
Match m = Regex.Match(input, pattern);
if(m.Success)
{
Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m);
}
else Console.WriteLine("Input: \"{0}\" did not match.", input);
}





}


}
 
dang...my head hurts just looking at that....lol

so what does it do? lol


Michael said:
In case you're still curious:

"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$"

Example Code:

public class App1
{

[STAThread]
static void Main()
{
string pattern =
@"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$";
string[] inputs = {"( ( A * B ) / C) - 10 ", " ) ( A * B ( / C ( -
10", "( ( A * B / C) - 10 ", "()", "(()", "(()))"};

foreach(string input in inputs)
{
Match m = Regex.Match(input, pattern);
if(m.Success)
{
Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m);
}
else Console.WriteLine("Input: \"{0}\" did not match.", input);
}





}


}


That worked perfectly. I was just checking in the wrong place.

THANKS!!

cbmeekshttp://www.codershangout.com
 
so what does it do? lol

It uses a lot of processor. Brian's solution is perfect.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

There is a madness to my method.

cbmeeks said:
dang...my head hurts just looking at that....lol

so what does it do? lol


Michael said:
In case you're still curious:

"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$"

Example Code:

public class App1
{

[STAThread]
static void Main()
{
string pattern =
@"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$";
string[] inputs = {"( ( A * B ) / C) - 10 ", " ) ( A * B ( / C ( -
10", "( ( A * B / C) - 10 ", "()", "(()", "(()))"};

foreach(string input in inputs)
{
Match m = Regex.Match(input, pattern);
if(m.Success)
{
Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m);
}
else Console.WriteLine("Input: \"{0}\" did not match.", input);
}





}


}


That worked perfectly. I was just checking in the wrong place.

THANKS!!

cbmeekshttp://www.codershangout.com

(e-mail address removed) wrote:
cbmeeks wrote:
Thanks! That is an excellent idea.

However, the following is returning OK:

( ) ) (

because it is zero.

I think after I pass the first check I will go through and do the
other
check mentioned earler

cbmeeks
http://www.codershangout.com

rossum wrote:
On 8 Dec 2006 09:50:17 -0800, "cbmeeks" <[email protected]>
wrote:

I have a project that requires me to validate the syntax of
basic math
formulas.

Something like: ( ( A * B ) / C) - 10

I need to just make sure that parenthesis are correct.

For example, you cant have a formula like: ) ( A * B ( / C ( -
10

Checking for equal number of ( as ) is easy.

Thanks!

cbmeeks
http://www.codershangout.com

No need for a regular expression if you are just checking
parentheses.
Start a count at 0. Every time you hit '(' increment the count.
Every time you hit ')' decrement the count. If the count goes
negative then you have a ')' before its corresponding '('. If
the
count is not zero at the end of the expression then you have a
'('
without a corresponding ')'. If the count is zero at the end of
the
expression then the parentheses are well formed.

rossum

Add the condition that the "count" must never be less than zero as it
scans. Your example of ( ) ) ( will be negative -1 before it hits the
end, specifically just after the second ')' character.
 
Correct. It is working perfectly and fast.

Thanks Brian and everyone else!

cbmeeks
http://www.codershangout.com


Kevin said:
so what does it do? lol

It uses a lot of processor. Brian's solution is perfect.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

There is a madness to my method.

cbmeeks said:
dang...my head hurts just looking at that....lol

so what does it do? lol


Michael said:
In case you're still curious:

"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$"

Example Code:

public class App1
{

[STAThread]
static void Main()
{
string pattern =
@"^[^\(\)]*(((?<OpenParen>\()[^\(\)]*)+((?<CloseParen-OpenParen>\))[^\(\)]*)+)*(?(OpenParen)(?!))$";
string[] inputs = {"( ( A * B ) / C) - 10 ", " ) ( A * B ( / C ( -
10", "( ( A * B / C) - 10 ", "()", "(()", "(()))"};

foreach(string input in inputs)
{
Match m = Regex.Match(input, pattern);
if(m.Success)
{
Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m);
}
else Console.WriteLine("Input: \"{0}\" did not match.", input);
}





}


}


That worked perfectly. I was just checking in the wrong place.

THANKS!!

cbmeekshttp://www.codershangout.com

(e-mail address removed) wrote:
cbmeeks wrote:
Thanks! That is an excellent idea.

However, the following is returning OK:

( ) ) (

because it is zero.

I think after I pass the first check I will go through and do the
other
check mentioned earler

cbmeeks
http://www.codershangout.com

rossum wrote:
On 8 Dec 2006 09:50:17 -0800, "cbmeeks" <[email protected]>
wrote:

I have a project that requires me to validate the syntax of
basic math
formulas.

Something like: ( ( A * B ) / C) - 10

I need to just make sure that parenthesis are correct.

For example, you cant have a formula like: ) ( A * B ( / C ( -
10

Checking for equal number of ( as ) is easy.

Thanks!

cbmeeks
http://www.codershangout.com

No need for a regular expression if you are just checking
parentheses.
Start a count at 0. Every time you hit '(' increment the count.
Every time you hit ')' decrement the count. If the count goes
negative then you have a ')' before its corresponding '('. If
the
count is not zero at the end of the expression then you have a
'('
without a corresponding ')'. If the count is zero at the end of
the
expression then the parentheses are well formed.

rossum

Add the condition that the "count" must never be less than zero as it
scans. Your example of ( ) ) ( will be negative -1 before it hits the
end, specifically just after the second ')' character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

regular expression 7
Math Troubles 4
Regular Expressions question 5
Regular Expression help 2
Regular expression problem 1
Regular Expression Help 15
Regular Expression Help on syntax 19
Enumeration values 12

Back
Top