473,624 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11341
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4164
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
9
3145
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var strkey = "something"; var str = "Somethin like this"; if( str.search( / + strkey + / ) > -1 )
11
5366
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However, I want to use the text included within the brackets to do a lookup so that I can replace the expression with the new text. For example:
6
489
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address with a "&" character in the local part. I know I should be able to work it out myself, but I'd like to ask anyone to suggest the best way to
3
3209
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
2
3009
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and otherwise ignored. I decided to use the following regular expression to remove all punctuation (actually...
7
3816
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
3
16913
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it with: import fileinput, string, sys fileQuery = "Text.txt" sourceText = '''SOURCE'''
1
3386
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
3
1825
by: =?Utf-8?B?VEo=?= | last post by:
Hi, I want to know how Regular Expression can be used in this situation. I want to replace some string in specific condition.. The condition is to replace string only if the string is NOT inside of tag. For example, <Test id='wow'>wow</Test> I want to replace the wow with great...So output will be <Test id='wow'>great</Test>
0
8238
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.