2017-03-03 29 views
0

我有一份報告,我以純文本的形式給出了同事通常必須手動編輯各種標題的報告。我知道標題的最上面一行和最下面一行 - 它們在整個文檔中並沒有不同,但它們之間的各種文本行。 格式如下:vb.net正則表達式從報告中解析段落

BEGIN REPORT FOR CLIENT XXYYZZ 
RANDOM BODY TEXT 
RANDOM BODY TEXT 
RANDOM BODY TEXT 
RANDOM BODY TEXT 
RANDOM BODY TEXT 
FINAL REPORT 

我試圖使用正則表達式來突出豐富的文本框內這個文本。如果我使用下面的代碼我可以突出頂線的每一次出現沒有問題:

Dim mystring As String = "(BEGIN)(.+?)(XXYYZZ)" 
Dim regHeader As New Regex(mystring) 
Dim regMatch As Match = regHeader.Match(rtbMain.Text) 

While regMatch.Success 
    rtbMain.Select(regMatch.Index, regMatch.Length) 
    rtbMain.SelectionColor = Color.Blue 

    regMatch = regMatch.NextMatch() 
End While 

但是,一旦我試圖改變代碼找到全款不再將突出什麼。下面是我期待它的結果,但它不縫以任何理由喜歡它,並不會突出顯示任何東西:

Dim mystring As String = "(BEGIN REPORT FOR CLIENT XXYYZZ)(.+?)(FINAL REPORT)" 
Dim regHeader As New Regex(mystring) 
Dim regMatch As Match = regHeader.Match(rtbMain.Text) 

While regMatch.Success 
    rtbMain.Select(regMatch.Index, regMatch.Length) 
    rtbMain.SelectionColor = Color.Blue 

    regMatch = regMatch.NextMatch() 
End While 

任何幫助將不勝感激。

回答

0

你需要的是singleline mode,爲了讓.甚至匹配換行符。

試試這個:

Dim mystring As String = "(BEGIN REPORT FOR CLIENT XXYYZZ)(.+?)(FINAL REPORT)" 
Dim regHeader As New Regex(mystring, RegexOptions.Singleline) 
Dim regMatch As Match = regHeader.Match(rtbMain.Text) 

While regMatch.Success 
    rtbMain.Select(regMatch.Index, regMatch.Length) 
    rtbMain.SelectionColor = Color.Blue 

    regMatch = regMatch.NextMatch() 
End While 

通知RegexOptions.Singleline

+0

這解決了我的問題,謝謝一噸 – Matt