简体   繁体   English

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

[英]VBA regex - getting matches from multiple lines

Provided that I have the following text: 只要我有以下文字:

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

I would like to use the following pattern: 我想使用以下模式:

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

It returns matches only from the second line. 它仅从第二行返回匹配项。 But I want it to get matches from both lines : 但是我希望它能够从两行中获取匹配项 调试

Here is the code which I use: 这是我使用的代码:

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)

The solution was the following. 解决方法如下。

At the end of the first line, there is a newline character. 在第一行的末尾,有一个换行符。 In the document editor (outside VBA) newlines are not represented with vbNewLine, but it is a character of number 13. Those two are not the same, and for regexing vbNewLine must be used. 在文档编辑器(VBA外部)中,换行符未用vbNewLine表示,但是它是数字13的字符。这两个字符不相同,并且必须使用vbNewLine进行正则表达式。 So I had to replace it with vbNewLine. 所以我不得不用vbNewLine代替它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM