2016-12-02 42 views
2

獲得比賽只要我有以下文字:VBA正則表達式 - 從多條線路

55,8%(1) – 3426 bytes used from 6kB 
58,1%(2) – 3572 bytes used from 6kB 

我想用以下模式:

^(\d\d,\d)(?:%\(\d\) .)(\d{3,4})(?:)(bytes)(?: used from)(\d{1,3})(?:kB)$ 

它只返回從第二行匹配。但我想它從兩條線獲得比賽: debug

這裏是我使用的代碼:

Dim ramtext As String 
ramtext = getTableCellText("1.7", 3, 2) 

Dim regex As New RegExp 
With regex 
    .Pattern = "^(\d\d,\d)(?:%\(\d\) .)(\d{3,4})(?:)(bytes)(?: used from)(\d{1,3})(?:kB)$" 
    .Global = 1 
    .MultiLine = 1 
End With 


Dim matches As MatchCollection 
Set matches = regex.Execute(ramtext) 
+0

您確定一開始沒有垃圾字符嗎?嘗試在'^'後面加上'\ W *'。 (\ d {1,3})kB $「(\ d {3,4})(字節)中使用'\ W *(\ d \ d,\ d)%\ ' –

+0

仍然一樣。如果你在regexr.com上試試這個,你會發現這個模式本身有效。而且regexr可以識別兩行(你需要在那裏設置multiline = true) – ThomasMX

+0

你確定這些空格是正常的嗎?嘗試用'\ s'替換空格。嘗試'「^(\ d \ d,\ d)%\(\ d \)\ s。\ s(\ d {3,4})\ s(bytes)\ sused \ sfrom \ s(\ d {1 ,3})KB $「'。 –

回答

0

的解決方案是以下。

在第一行的末尾有一個換行符。在文檔編輯器(VBA之外)中,換行符不用vbNewLine表示,但是它是一個13號字符。這兩個不一樣,並且必須使用vbNewLine。所以我不得不用vbNewLine替換它。

ramtext = Replace(text, Chr(13), vbNewLine)