VBA正则表达式 - 从多行获取匹配

时间:2016-12-02 10:54:44

标签: regex 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)

1 个答案:

答案 0 :(得分:0)

解决方案如下。

在第一行的末尾,有一个换行符。在文档编辑器(VBA外部)中,换行符不用vbNewLine表示,但它是13号字符。这两个字符不相同,并且必须使用regexing vbNewLine。所以我不得不用vbNewLine替换它。

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