RegExp (gb.pcre)

这个类表示一个正则表达式,使用它可以对各种字符串执行匹配并检索子匹配(主题字符串中与带括号的表达式匹配的部分)。

该类是可创建

该类行为像一个只读数组。

常数
Anchored  
BadMagic  
BadOption  
BadUTF8  
BadUTF8Offset  
Callout  
Caseless  
DollarEndOnly  
DotAll  
Extended  
Extra  
Greedy  
MatchLimit  
MultiLine  
NoAutoCapture  
NoMatch  
NoMemory  
NoSubstring  
NoUTF8Check  
NotBOL  
NotEOL  
NotEmpty  
Null  
UTF8  
Ungreedy  
UnknownNode  

静态方法
FindAll  
Match  
Replace  

属性
Count  
Error  
Offset  
Pattern  
SubMatches  
Subject  
Text  

方法
Compile  
Exec  

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:

brown (\S+)

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:

brown fox

and its RegExp[1].Text property would give the text of the first submatch:

fox

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