RegExp (gb.pcre)
这个类表示一个正则表达式,使用它可以对各种字符串执行匹配并检索子匹配(主题字符串中与带括号的表达式匹配的部分)。
常数
静态方法
属性
方法
SubMatches
A submatch is a part of your pattern contained in parentheses.
To access the submatches use the Count property and the array accessor of the RegExp class.
Example
For example, given the regular expression:
and subject string:
The quick brown fox slyly jumped over the lazy dog
your Regexp object's
Text
(or RegEx[0].Text) property would be:
and its
RegExp[1].Text
property would give the text of the first submatch:
the offset in the string is given by the Offset property, 10 in this case.
This is just a simple example of what regular expressions can do for you when parsing textual input; they are a very powerful tool. For example, the following regular expression will extract valid email addresses for you:
(?i)\b[a-z0-9._%\-]+@[a-z0-9._%\-]+\.[A-Z]{2,4}\b
Sample program to parse the output of vmstat -D:
Dim sDiskIO, sVal As String
Dim cVal As New Collection
Dim rMatch As New RegExp
' get disk I/O stats
Exec ["vmstat", "-D"] To sDiskIO
For Each sVal In ["total reads", "read sectors", "writes", "written sectors"]
rMatch.Compile("^\\s*(\\d+)\\s+" & sVal, RegExp.MultiLine)
rMatch.Exec(sDiskIO)
If rMatch.Count = 1 Then
cVal[Replace(sVal, " ", "_")] = rMatch[1].Text
Else
Error.Raise("Missing '" & sVal & "' in 'vmstat -D' output")
Endif
Next
Print "total reads: " & cVal!total_reads & " read sectors:" & cVal!read_sectors
Print "writes: " & cVal!writes & " written sectors: " & cVal!written_sectors
See also