I have a complex situation where I need to parse a very long string. I have to look for a pattern in the string and replace that pattern with another. I know I can simply use find/replace
method but my case is bit different.
I have a string that contains the following pattern
#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
and I want it to format as
#EPB_IMG#index-1_1.jpg|index-1_2.jpg|index-1_3.jpg|index-1_4.jpg#EPB_IMG
I don't know much about Regex
and seeking for help.
Regex is overkill:
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.
Maybe something like this:
Expression: \\w+#(?<Image>(.+)\\.jpg)#\\w+
Replacement: ${Image}
Result: string.Format("#EPB_IMG#{0}#EPB_IMG", string.Join("|", listOfMatches))
NOTE: Regex tested with: http://regexhero.net/tester/
Result is untested, but should work!
var result = "#EPB_IMG" + Regex.Matches(inputString, @"#EPB_IMG(.+)#EPB_IMG")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.Aggregate((f, f2) => f + "|" + f2) + "#EPB_IMG";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.