2017-03-03 47 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