2013-11-03 36 views
1

我正在嘗試使用C#和Regex在RichTextBox中進行語音識別,以便當用戶單擊「查找語音」時,所有語音標記中的語音都將突出顯示藍色。但是,我不太確定如何將內部演講與正則表達式結合起來,因爲我目前所能做的就是突出顯示語音標記。C#從語音標記中獲取語音

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; 
    } 
} 
+2

你能告訴我們輸入的文本?你準備搜索/匹配什麼? –

+0

一個示例字符串將非常有用。 –

回答

1

試試這個:

Regex SpeechMatch = new Regex("\".+?\""); 
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 
+0

它沒有工作。我有多個編譯器錯誤;大部分字符串區域都加下劃線。我該怎麼辦? – Joe

+0

@Joe:這個想法就是這種模式,但我不確定你有多少次必須避免使用雙引號和反斜槓。 –

+0

@CasimiretHippolyte只需刪除@。當你使用它時,你不必逃避反斜槓,但你必須加倍雙引號 – Jerry