Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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中查找替换模式_C#_Regex_String - Fatal编程技术网

C# 在C中查找替换模式

C# 在C中查找替换模式,c#,regex,string,C#,Regex,String,我有一个复杂的情况,我需要解析一个很长的字符串。我必须在字符串中查找一个模式,并用另一个模式替换该模式。我知道我可以简单地使用find/replace方法,但我的情况有点不同 我有一个包含以下模式的字符串 #EPB_IMG#index-1_1.jpg#EPB_IMG #EPB_IMG#index-1_2.jpg#EPB_IMG #EPB_IMG#index-1_3.jpg#EPB_IMG #EPB_IMG#index-1_4.jpg#EPB_IMG 我希望它的格式为 #EPB_IM

我有一个复杂的情况,我需要解析一个很长的字符串。我必须在字符串中查找一个模式,并用另一个模式替换该模式。我知道我可以简单地使用find/replace方法,但我的情况有点不同

我有一个包含以下模式的字符串

#EPB_IMG#index-1_1.jpg#EPB_IMG


#EPB_IMG#index-1_2.jpg#EPB_IMG


#EPB_IMG#index-1_3.jpg#EPB_IMG


#EPB_IMG#index-1_4.jpg#EPB_IMG
我希望它的格式为

#EPB_IMG#index-1_1.jpg|index-1_2.jpg|index-1_3.jpg|index-1_4.jpg#EPB_IMG

我对Regex和寻求帮助了解不多。

可能是这样的:

表达式:\w+?。++\.jpg\w+

替换:${Image}

结果:string.FormatEPB_IMG{0}EPB_IMG,string.Join|,listOfMatches

注: 正则表达式使用以下参数进行测试:


结果未经测试,但应该有效

可能是这样的:

表达式:\w+?。++\.jpg\w+

替换:${Image}

结果:string.FormatEPB_IMG{0}EPB_IMG,string.Join|,listOfMatches

注: 正则表达式使用以下参数进行测试:

结果未经测试,但应该有效

正则表达式是一种过度使用:

var parts = s.Split(new string[] { "#EPB_IMG", "#", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
var result = string.Join("|", parts);

Console.WriteLine("#EPB_IMG#" + result + "#EPB_IMG"); // prints your result.
正则表达式是一种过度使用:

var parts = s.Split(new string[] { "#EPB_IMG", "#", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
var result = string.Join("|", parts);

Console.WriteLine("#EPB_IMG#" + result + "#EPB_IMG"); // prints your result.

我建议将正则表达式从这个问题的标题中删除。它假设的答案可能不是最好的。我建议将正则表达式从这个问题的标题中删除。它假设的答案可能不是最好的。