2013-06-03 38 views
0

我想使用正則表達式來查找文本的特殊部分。例如,我有這樣一個文本:KENNFELD TFSWNWRSA 4 4使用RegularExpression提取文本的特殊部分

我想只提取TFSWNWRSA 4 4從這段文字,而不是KENNFELD,然後我想

我寫了這個代碼,但它返回所有總行:

Dim fso As New FileSystemObject 
Dim ts As TextStream 
Dim Name As String 
Dim regx As New regexp 
Dim matchkennfeld As MatchCollection 
Dim matchname As MatchCollection 

Name = "D:/test_DC.txt" 
'Set regexp = CreateObject("vbscript.regexp") 
Set ts = fso.OpenTextFile(Name, ForReading) 
Do While Not ts.AtEndOfStream 
    regx.Pattern = "KENNFELD\s+([A-Z 0-9]*)" 
    Set matchkennfeld = regx.Execute(ts.ReadLine) 
    If matchkennfeld.Count <> 0 Then 
    regx.Pattern = "([A-Z 0-9]*)" 
    ' MsgBox matchkennfeld.Item(0) 
    Set matchname = regx.Execute(matchkennfeld.Item(0)) 
     For Each Match In matchname 
      MsgBox Match 
     Next Match 
End If 
Loop 

你能幫我做這份工作嗎?

+1

你的意思是[this](http://regex101.com/r/qV5jJ5)? – HamZa

+0

但是有了這個我有我想要的完整文本只有TFSWNWRSA 4 4 – TangoStar

+0

請使用'm'修飾符。 – HamZa

回答

1

我不擅長VB。但我會說matchkennfeld將是一個數組,它包含與組合一起的匹配(就像其他語言一樣)。所以當檢查Item(0)時,我認爲它與整個比賽相匹配,而不是組。所以改變它來檢查Sub匹配將解決這個問題。

Set matchname = regx.Execute(matchkennfeld(0).SubMatches(0))可能會解決這個問題。