0

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.

1
  • 1
    I'd suggest removing "Regex" from the title of this question. It supposes an answer that may not be best. Commented Jan 9, 2013 at 9:59

3 Answers 3

6

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

0
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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.