C# regular expression replace help ??

M

Mahesha

Hello,

I need help in replacing one string pattern with another.
Ex: I have a financial security expression like
log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)

Here "T 3.25 6/24/2004" is a variable on which I need to perform log
and then divide the result with 3.980. While parsing such expressions,
I need to find all occurence of date values and replace "/" character
with "~" character. I need to do this only for date portion of the
expression and not for divison operator.

After the transformation , this expression should be
log(T 3.25 6~24~2004)/sqrt(T 4.5 6~19~2002)

I was looking at replace method of regex object. But this doesn't
provide replacing one pattern with another pattern. I mean I'm not
able to figure out a way to find a date and replace all "/" characters
of the date with "~".

Regards,
Mahesha
 
K

Kristopher K. Kruger

string temps = log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)
temps = temps.Replace(@"/", @"~");
temps = temps.Replace(@"~sqrt", @"/sqrt");
 
C

Chayapathi Mahesha

Hello,

In this example log and sqrt were just some examples. In practice the
expression can be any expression involving mathematical operators and
functions...

I'm looking at a more generic solution...

Thanks,
Mahesha


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
R

Rahul Anand

Hi Mahesha,

You can use "Replace" method available under "Regex" Class
to replace a matched string with replacement string.

In your code first extract the "Date" part and then
replace it with formatted date string.

The following code snippet might help you:

[SNIP]

string s = @"log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)";
Regex r = new Regex(@"([\d]{1,2})/([\d]{1,2})/([\d]{4})");
s = r.Replace(s,"$1~$2~$3");

[/SNIP]
 
R

Rahul Anand

You can also use MatchEvaluator (.NET Delegates) to
replace the matched string.

[SNIP]

string MatchHandler( Match m)
{
return m.Groups[0].Value.Replace("/","~");
}


MatchEvaluator me = new MatchEvaluator(MatchHandler);
string s = @"log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)";
Regex r = new Regex(@"[\d]{1,2}/[\d]{1,2}/[\d]{4}");
s = r.Replace(s,me);

[/SNIP]

I guess the first solution is more efficient, whereas this
solution is more elegant.

--
Cheers,
Rahul
-----Original Message-----
Hi Mahesha,

You can use "Replace" method available under "Regex" Class
to replace a matched string with replacement string.

In your code first extract the "Date" part and then
replace it with formatted date string.

The following code snippet might help you:

[SNIP]

string s = @"log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)";
Regex r = new Regex(@"([\d]{1,2})/([\d]{1,2})/([\d]{4})");
s = r.Replace(s,"$1~$2~$3");

[/SNIP]

--
Cheers,
Rahul
-----Original Message-----
Hello,

I need help in replacing one string pattern with another.
Ex: I have a financial security expression like
log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)

Here "T 3.25 6/24/2004" is a variable on which I need to perform log
and then divide the result with 3.980. While parsing
such
expressions,
I need to find all occurence of date values and replace "/" character
with "~" character. I need to do this only for date portion of the
expression and not for divison operator.

After the transformation , this expression should be
log(T 3.25 6~24~2004)/sqrt(T 4.5 6~19~2002)

I was looking at replace method of regex object. But
this
doesn't
provide replacing one pattern with another pattern. I mean I'm not
able to figure out a way to find a date and replace all "/" characters
of the date with "~".

Regards,
Mahesha
.
.
 

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

Top