Home > Software engineering >  Using regex.replace() to add to ", and "to the third to last comma in a string
Using regex.replace() to add to ", and "to the third to last comma in a string

Time:10-28

I have a two scenarios which occur in my code:

  1. A string that consist of just names and the last item does not have a designation (", CPA, CFA"). For instance, "John, Jan, Joe" and I use the code below to replace the last comma with and so I get "John, Jan and Joe"
  2. A string that consist of names and a CPA/CFA designation (", CPA, CFA") at the end such as "John, Jan, Joe, CPA, CFA". In this scenario I need to replace the third to the end commma with and to get "John, Jan, and Joe, CPA, CFA". I just need to handle the case of that third to the end comma. I will note these are just sample strings and really it could include more names (i.e., it could "jake, jan, joe, john, jessie") but ultimately I am just checking if that last name has the designations(extra commas) and if so it should account for it by only adding the and to replace the third to last comma.

My goal is to properly add the and to the last item in the comma separated list to follow standard English practices. The designation commas for the last item throw off my Regex expression I was using to add that last and in replacement of the comma.

My code:

if(str1.EndsWith(", CPA, CFA"))
{
       //need to figure out
}
else
{
        Regex.Replace(str1, ", ([^,] )$", " and $1");
}

CodePudding user response:

You might use a pattern with 2 capture groups capturing the first comma and match the second comma

The the replacement use the 2 groups $1 and $2

([^\s,] ),\s*([^\s,] (?:, CPA, CFA)?)$

The pattern matches:

  • ([^\s,] ) Capture group 1, match 1 non whitespace chars and a comma

  • ,\s* Match a comma and optional whitespace chars

  • ( Capture group 2

    • [^\s,] Match 1 chars other than a whitespace char or comma
    • (?:, CPA, CFA)? Optionally match , CPA, CFA
  • ) Close group 2

  • $ End of string

Regex demo | C# demo

Example

string pattern = @"([^\s,] ),\s*([^\s,] (?:, CPA, CFA)?)$";
string input = "John, Jan, Joe\nJohn, Jan, Joe, CPA, CFA\njake, jan, joe, john, jessie, jack, jones";
Console.WriteLine(Regex.Replace(input, pattern, @"$1 and $2", RegexOptions.Multiline));

Output

John, Jan and Joe
John, Jan and Joe, CPA, CFA
jake, jan, joe, john, jessie, jack and jones

CodePudding user response:

You can use the following code without using Regex

//string input = "John, Jan, Joe, CPA, CFA";
string input = "John, Jan, Joe";
var result = input.Select((b, i) => b.Equals(',') ? i : -1).Where(i => i != -1).ToList();

if(input.EndsWith(", CFA"))
     input=input.Replace(input.Substring(0,result[result.Count()-3]  2), input.Substring(0, result[result.Count()-3] 2)   "and ");
else
     input = input.Replace(input.Substring(0, result[result.Count()-1]  1), input.Substring(0, result[result.Count()-1])   " and ");

Console.WriteLine(input); 

if string input = "John, Jan, Joe, CPA, CFA":

result: "John, Jan, and Joe, CPA, CFA" (Apparently, you have added an extra comma)

if string input = "John, Jan, Joe";

result: John, Jan and Joe

  • Related