473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

regex multiplication problem

The following is working for me but I want to include numbers in scientific
notation.

public double Evaluate( string expr )
{
const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)"
Regex reMulDiv = new Regex(Num + @"\s*([*/])\s*" + Num);

other stuff:

while ( reMulDiv.IsMatch( expr ) )
{
Regex restar = new Regex(@"\*");
string bstr = reMulDiv.Match(expr).Value.ToString();
bstr = restar.Replace(bstr, "\\*");
string a=reMulDiv.Match(expr).Groups.SyncRoot.ToString();
string astr = DoMulDiv(reMulDiv.Match(expr));
Regex rx = new Regex(bstr);
expr = rx.Replace(expr, astr);
}

other stuff
return (Convert.ToDouble(expr));
}

public string DoMulDiv( Match m )
{
int i = 0;

double n1 = Convert.ToDouble(m.Groups[1].Value);
double n2 = Convert.ToDouble(m.Groups[3].Value);

switch (m.Groups[2].Value.ToString())
{
case "/":
return ( n1 / n2 ).ToString();
case "*":
return ( n1 * n2 ).ToString();
default:
return "";
}
}

Trying to scale-up to include numbers in sci notation.
const string Num = @"((\-?\d+\.?\d*|\-?\.\d+)([E][-+]?[0-9]+)?)";

Trouble is n1, n2 ,n3 are nolonger the first#, the operator, and the last#.
The number of groups varies and the position of the numbers and operator is
not predictable. I think my problem is that I am not implementing the
function properly. But the rework (without a hack) is evading me.

please help

Aug 31 '08 #1
3 2165
Mark_B wrote:
The following is working for me but I want to include numbers in scientific
notation.

public double Evaluate( string expr )
{
const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)"
Regex reMulDiv = new Regex(Num + @"\s*([*/])\s*" + Num);

other stuff:

while ( reMulDiv.IsMatch( expr ) )
{
Regex restar = new Regex(@"\*");
string bstr = reMulDiv.Match(expr).Value.ToString();
bstr = restar.Replace(bstr, "\\*");
string a=reMulDiv.Match(expr).Groups.SyncRoot.ToString();
string astr = DoMulDiv(reMulDiv.Match(expr));
Regex rx = new Regex(bstr);
expr = rx.Replace(expr, astr);
}

other stuff
return (Convert.ToDouble(expr));
}

public string DoMulDiv( Match m )
{
int i = 0;

double n1 = Convert.ToDouble(m.Groups[1].Value);
double n2 = Convert.ToDouble(m.Groups[3].Value);

switch (m.Groups[2].Value.ToString())
{
case "/":
return ( n1 / n2 ).ToString();
case "*":
return ( n1 * n2 ).ToString();
default:
return "";
}
}

Trying to scale-up to include numbers in sci notation.
const string Num = @"((\-?\d+\.?\d*|\-?\.\d+)([E][-+]?[0-9]+)?)";

Trouble is n1, n2 ,n3 are nolonger the first#, the operator, and the last#.
The number of groups varies and the position of the numbers and operator is
not predictable. I think my problem is that I am not implementing the
function properly. But the rework (without a hack) is evading me.
I think you need something more powerful than regex.

What you are asking for is a standard feature in a lexical scanner
and parser.

Find a lexical scanner and parser generator for .NET, specify
a grammar for what you want and generate the code.

Arne

Aug 31 '08 #2
Mark_B wrote:
The following is working for me but I want to include numbers in scientific
notation.

public double Evaluate( string expr )
{
const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)"
Regex reMulDiv = new Regex(Num + @"\s*([*/])\s*" + Num);

other stuff:

while ( reMulDiv.IsMatch( expr ) )
{
Regex restar = new Regex(@"\*");
string bstr = reMulDiv.Match(expr).Value.ToString();
bstr = restar.Replace(bstr, "\\*");
string a=reMulDiv.Match(expr).Groups.SyncRoot.ToString();
string astr = DoMulDiv(reMulDiv.Match(expr));
Regex rx = new Regex(bstr);
expr = rx.Replace(expr, astr);
}

other stuff
return (Convert.ToDouble(expr));
}

public string DoMulDiv( Match m )
{
int i = 0;

double n1 = Convert.ToDouble(m.Groups[1].Value);
double n2 = Convert.ToDouble(m.Groups[3].Value);

switch (m.Groups[2].Value.ToString())
{
case "/":
return ( n1 / n2 ).ToString();
case "*":
return ( n1 * n2 ).ToString();
default:
return "";
}
}

Trying to scale-up to include numbers in sci notation.
const string Num = @"((\-?\d+\.?\d*|\-?\.\d+)([E][-+]?[0-9]+)?)";

Trouble is n1, n2 ,n3 are nolonger the first#, the operator, and the last#.
The number of groups varies and the position of the numbers and operator is
not predictable. I think my problem is that I am not implementing the
function properly. But the rework (without a hack) is evading me.
For one thing, you can always use named groups:

const string Num = @"\-?\d+\.?\d*|\-?\.\d+"
Regex reMulDiv = new Regex("(?<larg>" + Num + @")\s*(?<op>[*/])\s*(?
<rarg>" + Num + ")");
...
double n1 = Convert.ToDouble(m.Groups["larg"].Value);
double n2 = Convert.ToDouble(m.Groups["rarg"].Value);
switch (m.Groups["op"].Value.ToString())

However, I would also recommend you to use a proper parser generator
for this task, rather than trying to hack one on your own with regex.
Coco/R (http://www.ssw.uni-linz.ac.at/coco/) is LL(1) - which is quite
enough to parse arithmetic expressions - easy to use, has good
documentation and is rather lightweight; ANTLR (http://antlr.org) is
very powerful - LL(*) - but has a somewhat steeper learning curve, and
is slower; and there are plenty more if you look around.
Sep 1 '08 #3
Thank you Pavel

"Pavel Minaev" wrote:
Mark_B wrote:
The following is working for me but I want to include numbers in scientific
notation.

public double Evaluate( string expr )
{
const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)"
Regex reMulDiv = new Regex(Num + @"\s*([*/])\s*" + Num);

other stuff:

while ( reMulDiv.IsMatch( expr ) )
{
Regex restar = new Regex(@"\*");
string bstr = reMulDiv.Match(expr).Value.ToString();
bstr = restar.Replace(bstr, "\\*");
string a=reMulDiv.Match(expr).Groups.SyncRoot.ToString();
string astr = DoMulDiv(reMulDiv.Match(expr));
Regex rx = new Regex(bstr);
expr = rx.Replace(expr, astr);
}

other stuff
return (Convert.ToDouble(expr));
}

public string DoMulDiv( Match m )
{
int i = 0;

double n1 = Convert.ToDouble(m.Groups[1].Value);
double n2 = Convert.ToDouble(m.Groups[3].Value);

switch (m.Groups[2].Value.ToString())
{
case "/":
return ( n1 / n2 ).ToString();
case "*":
return ( n1 * n2 ).ToString();
default:
return "";
}
}

Trying to scale-up to include numbers in sci notation.
const string Num = @"((\-?\d+\.?\d*|\-?\.\d+)([E][-+]?[0-9]+)?)";

Trouble is n1, n2 ,n3 are nolonger the first#, the operator, and the last#.
The number of groups varies and the position of the numbers and operator is
not predictable. I think my problem is that I am not implementing the
function properly. But the rework (without a hack) is evading me.

For one thing, you can always use named groups:

const string Num = @"\-?\d+\.?\d*|\-?\.\d+"
Regex reMulDiv = new Regex("(?<larg>" + Num + @")\s*(?<op>[*/])\s*(?
<rarg>" + Num + ")");
...
double n1 = Convert.ToDouble(m.Groups["larg"].Value);
double n2 = Convert.ToDouble(m.Groups["rarg"].Value);
switch (m.Groups["op"].Value.ToString())

However, I would also recommend you to use a proper parser generator
for this task, rather than trying to hack one on your own with regex.
Coco/R (http://www.ssw.uni-linz.ac.at/coco/) is LL(1) - which is quite
enough to parse arithmetic expressions - easy to use, has good
documentation and is rather lightweight; ANTLR (http://antlr.org) is
very powerful - LL(*) - but has a somewhat steeper learning curve, and
is slower; and there are plenty more if you look around.
Sep 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
2
by: Ranier Dunno | last post by:
Hi, I'm wondering how to write a regex matching a sentence like: "I'm eating a (banana|apple) for my health." Also, I'd like to know if there's a shorthand for multiplication of a single...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
5
by: scan87 | last post by:
Can somebody please help me with the following problem. I need to submit the problem on Monday. A program is required which could be used to help a child practice their multiplication tables. The...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.