Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have string with contains n number words, with comma separated , i want o replace those words another word using

ex:
string words="samuel king,rani queen,sastry poet";
i want to replace this words as
string words2="team,team,team";
with regular expression can u guide me or send snippets
Posted
Comments
DamithSL 17-Jun-14 3:08am    
how many maximum number of columns/items you have as words?

1 solution

Try:
C#
public static Regex regex = new Regex(
      "(?<Words>[\\w\\s]+)(?<Term>,|$)",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


public static string regexReplace =
      "team${Term}";

string words="samuel king,rani queen,sastry poet";
string result = regex.Replace(words,regexReplace);
 
Share this answer
 
Comments
kingsa 17-Jun-14 3:23am    
Thanks U all
OriginalGriff 17-Jun-14 3:26am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900