Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式替换字符串_C#_Regex_String - Fatal编程技术网

C# 使用正则表达式替换字符串

C# 使用正则表达式替换字符串,c#,regex,string,C#,Regex,String,我有这根绳子 string str = "5595176&cn=ANY&ln=ADdedfr"; 我需要更换12345的任何一个 string str = "5595176&cn=12345&ln=ADdedfr 任何字符串都可能不同。这可能会对您有所帮助 using System.Text.RegularExpressions; string mystr = Regex.Replace(str, "cn=[A-Za-z]+", "cn=12345"); 根

我有这根绳子

string str = "5595176&cn=ANY&ln=ADdedfr";
我需要更换12345的任何一个

string str = "5595176&cn=12345&ln=ADdedfr

任何字符串都可能不同。

这可能会对您有所帮助

using System.Text.RegularExpressions;
string mystr = Regex.Replace(str, "cn=[A-Za-z]+", "cn=12345");
根据评论,可能是

using System.Text.RegularExpressions;
string mystr = Regex.Replace(str, "cn=[A-Za-z]+", "cn=" + CountryCode);

我不想替换任何一个,任何一个都可以不同。。?它的前缀总是cn=?你一定要用抹布吗?为什么不拆下来换一个什么的呢。还有任何长度总是相同的吗?如果它可以不同,我们怎么知道要替换什么。@Mohithrivastava但它说任何东西都可以不同,这就是为什么你需要更多信息:)但是regex在这里听起来不必要。我有JS解决方案)请查看atlink=link.replaceAll(“cn=[A-Za-z]+,“cn=“+countryCode”);谢谢你。好。工作)乐意帮忙。:)@IgorStrekha愉快的编码荣誉。:)