1

我正在尝试使用 C# 和 Regex 在 RichTextBox 中进行语音识别,这样当用户单击“查找语音”时,所有语音标记和里面的语音都以蓝色突出显示。但是,我不太确定如何将查找内部语音与正则表达式结合起来,因为我目前所能做的就是突出显示语音标记。

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;
   }
}
4

2 回答 2

1

您可以使用此模式。主要的兴趣是它可以匹配引号内的转义引号:

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

图案细节:

\"             # 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
于 2013-11-03T16:53:13.540 回答
1

试试这个:

Regex SpeechMatch = new Regex("\".+?\"");
于 2013-11-04T21:15:49.593 回答