?
Solved

Help on Creating a C# Regex.Replace expression

Posted on 2011-03-25
7
Medium Priority
?
642 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 2000 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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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