请使用正则表达式替换字符串 [英] HOw to replace string using regular expression

查看:107
本文介绍了请使用正则表达式替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的字符串中包含n个单词,逗号分隔,我想要用另外的单词替换这些单词



ex:

string words =samuel king,rani queen,sastry poet;

i想要替换这个词为

string words2 = 团队,团队,团队;
带正则表达式的
可以指导我或发送片段

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

推荐答案

尝试:

Try:
public static Regex regex = new Regex(
      "(?<Words>[\\w\\s]+)(?<Term>,|


),
RegexOptions.IgnoreCase
| RegexOptions。多行
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);


public static string regexReplace =
team
)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled ); public static string regexReplace = "team


{期限};

string words = samuel king,rani queen,sastry poet;
string result = regex.Replace(words,regexReplace);
{Term}"; string words="samuel king,rani queen,sastry poet"; string result = regex.Replace(words,regexReplace);


这篇关于请使用正则表达式替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆