Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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 - Fatal编程技术网

C# 如何在特定位置用匹配的正则表达式模式替换字符串

C# 如何在特定位置用匹配的正则表达式模式替换字符串,c#,regex,C#,Regex,我有一个基本程序,它匹配字符串中的一个特定模式,然后将所有匹配项存储在一个数组中。然后我将每个匹配的元素附加到一个字符串中 现在我的问题是,如何用基于regex生成的修改字符串替换原始字符串中的匹配模式 可在此处找到示例工作程序: 正如您所看到的,生成的最后一个字符串只有最后修改的字符串。如何替换替换中的相应匹配项 代码: 您的代码可以简化为 string url = "Please ab123456 this is and also bc789456 and also de456789 ";

我有一个基本程序,它匹配字符串中的一个特定模式,然后将所有匹配项存储在一个数组中。然后我将每个匹配的元素附加到一个字符串中

现在我的问题是,如何用基于
regex
生成的修改字符串替换原始字符串中的匹配模式

可在此处找到示例工作程序:

正如您所看到的,生成的最后一个字符串只有最后修改的字符串。如何替换替换中的相应匹配项

代码:


您的代码可以简化为

string url = "Please ab123456 this is and also bc789456 and also de456789 ";
string[] queryString = Regex.Matches(url,@"\w{2}\d{6}").Cast<Match>().Select(x => x.Value).ToArray();
string finalurl=Regex.Replace(url,@"\w{2}\d{6}", "replace $&");
Console.WriteLine(finalurl); // => Please replace ab123456 this is and also replace bc789456 and also replace de456789

您能给我们提供更多关于所需输出字符串的信息吗?因为现在您编写的程序完全执行已编写的程序,所以它会将与正则表达式匹配的每个事件都替换为字符串“formatted…”。那么,实际需要的输出应该是什么,我们应该从输入字符串的哪里开始呢?目前,您最好只使用
string finalurl=Regex.Replace(url,@“\w{2}\d{6}”,“Replace$&”)。不需要任何for循环。也不需要
getMatch
,只需使用
string[]queryString=Regex.Matches(url@“\w{2}\d{6}”).Cast().Select(x=>x.Value.ToArray()
,请参见@WiktorStribiżew您的第一条注释很好,并按预期工作,但它不使用第二条注释,即
queryString
变量。有没有办法跟踪匹配中的索引并相应地替换它们?这是刚出炉的curiosity@RahulSharma您需要做什么?关键是,一旦使用正确的
Regex,就不需要该变量。这里不需要替换
syntax
queryString
,因为您在代码中不使用该变量。可能会减少执行时间和内存成本bit@FrancescoGimignano这就是为什么我添加了(如果您需要匹配值数组)如果用户同时需要匹配项和新字符串,则可以将其保留在答案中。
replace ab123456

replace bc789456

replace de456789

Please replace de456789 this is and also replace de456789 and also replace de456789
string url = "Please ab123456 this is and also bc789456 and also de456789 ";
string[] queryString = Regex.Matches(url,@"\w{2}\d{6}").Cast<Match>().Select(x => x.Value).ToArray();
string finalurl=Regex.Replace(url,@"\w{2}\d{6}", "replace $&");
Console.WriteLine(finalurl); // => Please replace ab123456 this is and also replace bc789456 and also replace de456789
string finalurl=Regex.Replace(url,@"\w{2}\d{6}", m =>
    SomeMethod(m.Value);
);