469,609 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

C# regular expression replace help ??

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
Nov 16 '05 #1
5 11168
string temps = log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002)
temps = temps.Replace(@"/", @"~");
temps = temps.Replace(@"~sqrt", @"/sqrt");


Mahesha wrote:
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

Nov 16 '05 #2
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!
Nov 16 '05 #3
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 logand then divide the result with 3.980. While parsing such expressions,I need to find all occurence of date values and replace "/" characterwith "~" character. I need to do this only for date portion of theexpression 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'tprovide replacing one pattern with another pattern. I mean I'm notable to figure out a way to find a date and replace all "/" charactersof the date with "~".

Regards,
Mahesha
.

Nov 16 '05 #4
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" Classto 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 toperform log
and then divide the result with 3.980. While parsing

suchexpressions,
I need to find all occurence of date values andreplace "/" 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

thisdoesn'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
.

.

Nov 16 '05 #5
Hi Rahul,
That was it..
Thanks a lot...
Regards,
Mahesha

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Dimitris Georgakopuolos | last post: by
6 posts views Thread by JohnSouth | last post: by
3 posts views Thread by James D. Marshall | last post: by
2 posts views Thread by Brian Kitt | last post: by
7 posts views Thread by Billa | last post: by
1 post views Thread by NvrBst | last post: by
3 posts views Thread by =?Utf-8?B?VEo=?= | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.