0

I use a C# Winforms RichTextBox to load and show some logfiles. Those logfiles uses ANSI escape chars to colorize the logfiles.

Found some examples to find and highlight a search string but I want to search for start and end strings, get those selections and colorize the content between.

Example: previous text ESC[36m SOME LOG CONTENT ESC[0m Some more text

So I can load and search for the strings, but I wasn't successful to create a function that search for ESC[36m as first string and ESC[0mas second string and then return the TextRange of it so I can highlight it after.

UPDATE To clarify, I do not just need the text between string-search-1 and string-search-2, I need a TextRange that selects the text so I can modify formatting.

YvesR
  • 5,922
  • 6
  • 43
  • 70
  • I was in a very similar situation recently and ended up using a WebBrowser control. This would give you a lot more control over font, color, etc. Plus, it's possible to easily make it look exactly like a RichTextBox. – Hele Sep 09 '16 at 15:33
  • There are plenty of examples of finding a string between two strings on StackOverflow. Examples [1](http://stackoverflow.com/questions/17252615/get-string-between-two-strings-in-a-string) / [2](http://stackoverflow.com/questions/1717611/find-a-string-between-2-known-values) / [3](http://stackoverflow.com/questions/13780654/extract-all-strings-between-two-strings) – Equalsk Sep 09 '16 at 16:15
  • All those examples return the found string between two strings. I need the TextRange selection. – YvesR Sep 09 '16 at 19:38

2 Answers2

2

Try using regular expressions. This should do it.

rtb.Text = "previous text ESC[36m SOME LOG CONTENT ESC[0m Some more text";

Regex regex = new Regex(@"ESC\[36m(.*?)ESC\[0m", RegexOptions.Multiline);
foreach (Match m in regex.Matches(rtb.Text))
{
    rtb.Select(m.Index + 7, m.Value.Length - 13);
    rtb.SelectionColor = Color.Aqua;
}
fatmuis
  • 36
  • 2
0

You could do this to find the text in between:

        string test = "ESC[36m SOME LOG CONTENT ESC[0m Some more text";
        int FirstIndex =  test.IndexOf("ESC[36") + 7;            
        test = test.Substring(FirstIndex,(test.Length-(FirstIndex+1)));
        int LastIndex = test.IndexOf("ESC[0");
        test = test.Substring(0, LastIndex);
Robert Altman
  • 161
  • 11