使用 VB 脚本的多行正则表达式
Multiline REGEX using VB Script
我有这个文本文件需要检查:
} else if ("saveAssured".equals(ACTION)) {
Integer assuredNo = giisAssuredService.saveAssured(assured);
模式将包含一个变量:
var = "saveAssured"
reMethod.Pattern = """& var &""[.]equals\(ACTION\).{\n.\w+?.=.\w+?[.](\w+?)\(\w+?\)"
我需要从文本文件中捕获第二个 'saveAssured'。 '\n'(新行)似乎不起作用。我用对了吗?我还可以尝试哪些其他步骤?
http://www.regular-expressions.info/dot.html
JavaScript and VBScript do not have an option to make the dot match
line break characters. In those languages, you can use a character
class such as [\s\S]
to match any character. This character matches a
character that is either a whitespace character (including line break
characters), or a character that is not a whitespace character. Since
all characters are either whitespace or non-whitespace, this character
class matches any character.
然后看看 https://regex101.com/r/kH3aZ4/1
测试是针对 JavaScript 但由于它们具有相同的正则表达式风格,因此该模式也适用于 VBScript。
Dim reMethod
Set reMethod = New RegExp
reMethod.IgnoreCase = True
reMethod.Pattern = """saveAssured""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
VBScript 表达式中关于多行的信息有点不一致。 VBScript RegExp 对象确实支持它们,但 属性 没有很好的记录。
>### 旗帜###
在JScript正则表达式/abc/gim中,g指定全局标志,i指定忽略大小写标志,m指定多行标志。
In VBScript, you can specify these flags by setting the equivalent properties to True.
The following table shows the allowed flags.
JScript 标志 VBScript 属性 如果标志存在或 属性 为真
g 全局查找搜索字符串中所有出现的模式
而不仅仅是第一次出现。
>
i IgnoreCase 搜索不区分大小写。
>
m 多行 ^ 匹配 \n 或 \r 之后的位置,并且
$ 匹配 \n 或 \r 之前的位置。
>
无论标志是否存在或 属性 是否为 True,
^ 匹配搜索字符串开头的位置,
和 $ 匹配搜索字符串末尾的位置。
下面是使用MultiLine
的例子。
Option Explicit
Dim rx: Set rx = New RegExp
Dim matches, match
Dim data: data = Array("This is two lines of text", "This is the second line", "Another line")
Dim txt: txt = Join(data, vbCrLf)
With rx
.Global= True
.MultiLine = True
.Pattern= "line$"
End With
Set matches = rx.Execute(txt)
WScript.Echo "Results:"
For Each match In matches
WScript.Echo match.Value
Next
输出:
Results:
line
line
MultiLine
设置为False
的区别。
输出:
Results:
line
我有这个文本文件需要检查:
} else if ("saveAssured".equals(ACTION)) {
Integer assuredNo = giisAssuredService.saveAssured(assured);
模式将包含一个变量:
var = "saveAssured"
reMethod.Pattern = """& var &""[.]equals\(ACTION\).{\n.\w+?.=.\w+?[.](\w+?)\(\w+?\)"
我需要从文本文件中捕获第二个 'saveAssured'。 '\n'(新行)似乎不起作用。我用对了吗?我还可以尝试哪些其他步骤?
http://www.regular-expressions.info/dot.html
JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as
[\s\S]
to match any character. This character matches a character that is either a whitespace character (including line break characters), or a character that is not a whitespace character. Since all characters are either whitespace or non-whitespace, this character class matches any character.
然后看看 https://regex101.com/r/kH3aZ4/1
测试是针对 JavaScript 但由于它们具有相同的正则表达式风格,因此该模式也适用于 VBScript。
Dim reMethod
Set reMethod = New RegExp
reMethod.IgnoreCase = True
reMethod.Pattern = """saveAssured""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
VBScript 表达式中关于多行的信息有点不一致。 VBScript RegExp 对象确实支持它们,但 属性 没有很好的记录。
>### 旗帜### 在JScript正则表达式/abc/gim中,g指定全局标志,i指定忽略大小写标志,m指定多行标志。
In VBScript, you can specify these flags by setting the equivalent properties to True.
The following table shows the allowed flags.
JScript 标志 VBScript 属性 如果标志存在或 属性 为真
g 全局查找搜索字符串中所有出现的模式 而不仅仅是第一次出现。 > i IgnoreCase 搜索不区分大小写。 > m 多行 ^ 匹配 \n 或 \r 之后的位置,并且 $ 匹配 \n 或 \r 之前的位置。 > 无论标志是否存在或 属性 是否为 True, ^ 匹配搜索字符串开头的位置, 和 $ 匹配搜索字符串末尾的位置。
下面是使用MultiLine
的例子。
Option Explicit
Dim rx: Set rx = New RegExp
Dim matches, match
Dim data: data = Array("This is two lines of text", "This is the second line", "Another line")
Dim txt: txt = Join(data, vbCrLf)
With rx
.Global= True
.MultiLine = True
.Pattern= "line$"
End With
Set matches = rx.Execute(txt)
WScript.Echo "Results:"
For Each match In matches
WScript.Echo match.Value
Next
输出:
Results:
line
line
MultiLine
设置为False
的区别。
输出:
Results:
line