Pergunta

I am trying to do speech recognition in a RichTextBox using C# and Regex, so that when the user clicks "Find Speech", all of the speech marks and the speech inside is highlighted in blue. However, I am not too sure of how to combine finding the inside speech with Regex, as all I can currently do is highlight the speech marks.

public void FindSpeech()
{ 

   Regex SpeechMatch = new Regex("\"");

   TXT.SelectAll();
   TXT.SelectionColor = System.Drawing.Color.Black;
   TXT.Select(TXT.Text.Length, 1);
   int Pos = TXT.SelectionStart;

   foreach (Match Match in SpeechMatch.Matches(TXT.Text))
   {
           TXT.Select(Match.Index, Match.Length);
           TXT.SelectionColor = System.Drawing.Color.Blue;
           TXT.SelectionStart = Pos;
           TXT.SelectionColor = System.Drawing.Color.Black;
   }
}
Foi útil?

Solução

Try this:

Regex SpeechMatch = new Regex("\".+?\"");

Outras dicas

You can use this pattern. The main interest is that it can match escaped quotes inside quotes:

Regex SpeechMatch = new Regex(@"\"(?>[^\\\"]+|\\{2}|\\(?s).)+\"");

pattern details:

\"             # literal quote
(?>            # open an atomic(non-capturing) group
    [^\\\"]+   # all characters except \ and "
  |            # OR
    \\{2}      # even number of \ (that escapes nothing)
  |            # OR
    \\(?s).    # an escaped character
)+             # close the group, repeat one or more times (you can replace + by * if you want)
\"             # literal quote
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top