471,319 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Find specific pattern in a string

Hai ,

in a textbox am having text as
"(20/100)+pay1*pay2" .it's a formula. and stored in a particular
variable.
string strformula="(20/100)+pay1*pay2" ;

i've to substitute the value of the variable 'pay1' & 'pay2' and
finding the value of that strformula.
can any onr tell me how to find 'pay1' and 'pay2' in the variable
strformula. it's urgent and reply immediately.
Thanks in advance.

Feb 11 '06 #1
10 8834
Are they always going to be called "pay1" and "pay2"? Is the formula
always going to be the same? If not, what can change?

You need to give more examples of different formulas (if you can have
different formulas) in order that we know what the parameters of the
problem are. One example isn't enough.

Feb 11 '06 #2
the formula may be
pay5+pay6

or

(pay6*pay7)*20/100
or
it may take any alphanumeric combinations

Feb 11 '06 #3
<ks***********@gmail.com> wrote:
the formula may be
pay5+pay6

or

(pay6*pay7)*20/100
or
it may take any alphanumeric combinations


Do you know exactly what you need to replace, and that it won't occur
in another form? For instance, if you needed to replace "pay1" with
"5", would you need to cope with:

pay1+repay1

and avoid that becoming

5+re5

?

If not, just use string.Replace:

string changed = original.Replace ("pay1", "5");

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #4
hai,
in c#.net string.replace method is not availbale.

Feb 11 '06 #5
Yes... it most certainly is. Of course, it's String.Replace, not
string.replace, and it's an instance method, so you need an actual
string. You can invoke it exactly as Jon wrote it, assuming that you
have declared a string called "original".

Feb 11 '06 #6
Hmm... this post seems to have gotten lost. Reposting.

There are two ways to look at this problem.

First, perhaps the "variables" in the expression are always named "pay"
followed by a digit. You could use a regular expression to find them,
and then substitute their values for them.

However, this raises the larger question of where are you going to find
their values?

The other way to look at the problem is to decide first where to store
the values. I would suggest a Hashtable, where the variable names are
the keys and the entries are the values. Then all you do is go through
the string, looking for identifiers, and when you find one that is in
the Hashtable, subustitute the corresponding value.

Basically, the algorithm goes like this:

// Fill a Hashtable with variables and corresponding values
Hashtable variables = new Hashtable();
variables["pay1"] = 45.6;
variables["pay6"] = 100.2;
etc.

// Build up a new string with the same expression as the expression
string,
// but with the variables replaced by their values
string expression = "(pay1 * pay6) * 20 / 100";
StringBuilder outExpression = new StringBuilder();
Regex identifierRegex = new Regex("[a-zA-Z][a-zA-Z0-9]*");
int inputIndex = 0;
Match nextIdMatch = identifierRegex.Match(expression, inputIndex);
while (nextIdMatch.Success)
{
string identifier = nextIdMatch.Value;
if (nextIdMatch.Index != inputIndex)
{
// Append everything since the end of the last identifier up to
the start of this identifier
outExpression.Append(expression.Substring(inputInd ex,
nextIdMatch.Index - inputIndex));
}
if (variables.Contains(identifier))
{
// Append the variable value
outExpression.AppendFormat("{0}", variables[identifier]);
}
else
{
// Error: no such variable
}

// Move forward and try to match next identifier
inputIndex = nextIdMatch.Index + nextIdMatch.Length;
nextIdMatch = identifierRegex.Match(expression, inputIndex);
}
if (inputIndex < expression.Length)
{
outExpression.Append(expression.Substring(inputInd ex));
}

// outExpression will now contain "(45.6 * 100.2) * 20 / 100"

WARNING: This code is untested! I make no guarantees that it will work
exactly as written. It's just to give you an idea.

Feb 11 '06 #7
There are two ways to look at this problem.

First, perhaps the "variables" in the expression are always named "pay"
followed by a digit. You could use a regular expression to find them,
and then substitute their values for them.

However, this raises the larger question of where are you going to find
their values?

The other way to look at the problem is to decide first where to store
the values. I would suggest a Hashtable, where the variable names are
the keys and the entries are the values. Then all you do is go through
the string, looking for identifiers, and when you find one that is in
the Hashtable, subustitute the corresponding value.

Basically, the algorithm goes like this:

// Fill a Hashtable with variables and corresponding values
Hashtable variables = new Hashtable();
variables["pay1"] = 45.6;
variables["pay6"] = 100.2;
etc.

// Build up a new string with the same expression as the expression
string,
// but with the variables replaced by their values
string expression = "(pay1 * pay6) * 20 / 100";
StringBuilder outExpression = new StringBuilder();
Regex identifierRegex = new Regex("[a-zA-Z][a-zA-Z0-9]*");
int inputIndex = 0;
Match nextIdMatch = identifierRegex.Match(expression, inputIndex);
while (nextIdMatch.Success)
{
string identifier = nextIdMatch.Value;
if (nextIdMatch.Index != inputIndex)
{
// Append everything since the end of the last identifier up to
the start of this identifier
outExpression.Append(expression.Substring(inputInd ex,
nextIdMatch.Index - inputIndex));
}
if (variables.Contains(identifier))
{
// Append the variable value
outExpression.AppendFormat("{0}", variables[identifier]);
}
else
{
// Error: no such variable
}

// Move forward and try to match next identifier
inputIndex = nextIdMatch.Index + nextIdMatch.Length;
nextIdMatch = identifierRegex.Match(expression, inputIndex);
}
if (inputIndex < expression.Length)
{
outExpression.Append(expression.Substring(inputInd ex));
}

// outExpression will now contain "(45.6 * 100.2) * 20 / 100"

WARNING: This code is untested! I make no guarantees that it will work
exactly as written. It's just to give you an idea.

Feb 11 '06 #8
ya your code is working fine.
now the expression is in string format.to get the value of that
expression, i am using the following snippet.it's throwing error of
input string was not in a correct format because the sring contains + (
- and etc.
am using the following snippet.

double ivalue = Convert .ToDouble(outExpression.ToString());
how to come over this?

Feb 11 '06 #9
Ahh. That's the hard part.

Convert.ToDouble (and the other, similar methods) are only to convert
individual string representations to individual values. So, for
example:

double x = Convert.ToDouble("0.0562");

What you need is an arithmetic expression evaluator. I believe that
VB.NET has an expression evaluator. C# doesn't, yet.

Does anyone know if the expression evaluation method is in the
VB-specific DLL? Is it callable from C#?

Otherwise you have to write your own expression evaluation engine. It's
not too terribly difficult, but it's not easy, either.

Feb 11 '06 #10
In thinking about Jon's reply using String.Replace, it occurred to me
that there's a much easier way to do this.

// Fill a Hashtable with variables and corresponding values
Hashtable variables = new Hashtable();
variables["pay1"] = 45.6;
variables["pay6"] = 100.2;
etc.

string expression = "(pay1 * pay6) * 20 / 100";

// Loop through the hashtable, replacing each variable with its value
string outExpression = expression;
foreach (DictionaryEntry de in variables)
{
string varName = (string)de.Key;
double varValue = (double)de.Value;
outExpression = outExpression.Replace(varName,
varValue.ToString());
}

// outExpression will now contain "(45.6 * 100.2) * 20 / 100"

MUCH easier to read! The only downside is that it doesn't catch
invalid variable names in the expression. Of course, you could always
test outExpression against a regular expression to ensure that it
contained no variable names:

Regex identifierRegex = new Regex("[a-zA-Z][a-zA-Z0-9]*");
Match nextIdMatch = identifierRegex.Match(outExpression);
if (nextIdMatch.Success) { ... still a variable name in the expression
.... }

Feb 11 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Greg Yasko | last post: by
3 posts views Thread by kittykat | last post: by
14 posts views Thread by micklee74 | last post: by
2 posts views Thread by graphicsxp | last post: by
14 posts views Thread by inpuarg | last post: by

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.