Solved

Help on Creating a C# Regex.Replace expression

Posted on 2011-03-25
7
616 Views
Last Modified: 2012-05-11
Hi all.

I´m trying to use Regex.Replace to replace the last comma on a string to 'and'. By eg:

"Im using laptop A, laptop B, laptop C".

After using Regex.Replace, I want:

"Im using laptop A, laptop B and laptop C".

How could I do this?
0
Comment
Question by:regisdaniel
7 Comments
 
LVL 5

Expert Comment

by:felipevidaurri
ID: 35218085
Try this code:

            string Text1 = "Im using laptop A, laptop B, laptop C";

            MessageBox.Show(Text1.Replace(",", " and"));
0
 

Author Comment

by:regisdaniel
ID: 35218103
Hi felipe.

I don´t wan´t to replace all ocurrences of comma on the string.
Only the LAST one.

"Im using laptop A, laptop B, laptop C".
"Im using laptop A, laptop B and laptop C".
0
 
LVL 5

Expert Comment

by:MikkelAStrojek
ID: 35218153
Don't know about regex but you could do the following instead:
string s = "Im using laptop A, laptop B, laptop C.";
            string tr = "Im using laptop A, laptop B and laptop C.";
            int i  =s.LastIndexOf(",");
            s = s.Remove(i,1);
            s = s.Insert(i, " and");
            Console.WriteLine(tr.Equals (s));

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:amitnepal
ID: 35218196
string Text1 = "Im using laptop A, laptop B, laptop C";

            MessageBox.Show(Text1.Replace(Text1.indexof(",",1), " and"));
0
 

Author Comment

by:regisdaniel
ID: 35218245
Hello MikkelAStrojek, thanks to your help.

But i really like to use Regex on this solution.
I found a example on the web:

http://stackoverflow.com/questions/3829483/how-to-replace-last-occurrence-of-characters-in-a-string-using-javascript

But I did´t realize a way to implement that on c#.

Thanks!
0
 
LVL 5

Accepted Solution

by:
MikkelAStrojek earned 500 total points
ID: 35218950
Ok... The below will do the trick.
string s1 = "Im using laptop A, laptop B, laptop C.";
s1 = System.Text.RegularExpressions.Regex.Replace(s1, @",(?=[^,]*\z)", " and");      

Open in new window

0
 

Author Closing Comment

by:regisdaniel
ID: 35256822
Great! Thanks!!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now