I am expecting to have a text as a string input in C# as shown under BEFORE_PROCESSING title. This text needs to be formatted such that:
As an example, after the formatting completed, the sentence in BEFORE_PROCESSING title should look like the text under AFTER_PROCESSING.
My question is what would be the most efficient way to realize this text processing business in C#? Would it be use of a regex or it overkills? Would you think some better alternative might exist? Thanks.
(I am using C#4)
BEFORE_PROCESSING
"Sentence 1 <style styles='B;fg:Green'>STYLED SENTENCE</style> Sentence 2"
AFTER_PROCESSING
"<style styles='B'>Sentence 1 </style>
<style styles='B;fg:Red'>STYLED SENTENCE</style>
<style styles='B'>Sentence 2</style>"
You can try the solution below, based on a regular expression :
string myLine = "Sentence 1<style styles='B;fg:Green'>STYLED SENTENCE</style>Sentence 2";
const string splitLinesRegex = @"((?<Styled>\<style[^\>]*\>[^\<\>]*\</style\>)|(?<NoStyle>[^\<\>]*))";
var splitLinesMatch = Regex.Matches(myLine, splitLinesRegex, RegexOptions.Compiled);
List<string> styledLinesBis = new List<string>();
foreach (Match item in splitLinesMatch)
{
if (item.Length > 0)
{
if (!string.IsNullOrEmpty(item.Groups["Styled"].Value))
styledLinesBis.Add(string.Format("<style styles='B'>{0}</style> ", item.Groups["Styled"].Value));
if (!string.IsNullOrEmpty(item.Groups["NoStyle"].Value))
styledLinesBis.Add(string.Format("<style styles='B;fg:Red'>{0}</style> ", item.Groups["NoStyle"].Value));
}
}
You just have to join the strings, using a string.Join statement for instance.