2016-12-02 44 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)