我正在尝试使用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;
}
}
答案 0 :(得分: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
答案 1 :(得分:1)
试试这个:
Regex SpeechMatch = new Regex("\".+?\"");