using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"
(?<firstcapture> ~Start [^~]* ) #_(3)
(?<randomStuff> .*? ) #_(4)
(?<loop_first> #_(5 start)
(?:
~
(?: LOOP )
\d [^~]
)
( .*? ) # (1)
) #_(5 end)
(?:
(?<loop_middle> #_(6 start)
(?:
(?:
~
(?: LOOP )
\d [^~]
)
.*?
)*
) #_(6 end)
(?<loop_last> #_(7 start)
(?:
~
(?: LOOP )
\d [^~]
)
( .*? ) # (2)
) #_(7 end)
)?
(?<end> ~End [ ] stuff~ ) #_(8)
";
string substitution = @"${firstcapture}${randomStuff}${loop_last}${loop_middle}${loop_first}${end}";
string input = @" BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP2 hello2~LOOP3 hello3~End stuff~
BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP2 hello2~End stuff~
BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP3 hello3~End stuff~
BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~End stuff~
";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx