Solved

Help on Creating a C# Regex.Replace expression

Posted on 2011-03-25
7
638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

622 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