Function RegMatch( ByVal S, pString) '返回正则匹配的集合 |
Dim regex As Object , matchs |
Dim Temp, n |
Set regex = CreateObject( "VBScript.RegExp" ) |
With regex |
.Global = True |
.IgnoreCase = True |
.Pattern = pString |
Set matchs = .Execute(S) |
End With |
Set regex = Nothing |
Set RegMatch = matchs |
Set matchs = Nothing |
End Function |
Function RegReplace(S, pstrt, rstr) '正则替换 |
Dim regex As Object |
Dim Temp |
Set regex = CreateObject( "VBScript.RegExp" ) |
With regex |
.Global = True |
.Pattern = pstrt |
Temp = .Replace(S, rstr) |
End With |
RegReplace = Temp |
Set regex = Nothing |
End Function |
Public Function RegGet(S, pString, Optional m = 0, Optional sp = "" ) |
'返回正则匹配\d[^_]*\d |
'm-是否多个 |
'sp-分隔符 |
Dim matchs, regex |
Dim ss, n |
Set regex = CreateObject( "VBScript.RegExp" ) |
With regex |
.Global = True |
.IgnoreCase = True |
.Pattern = pString |
Set matchs = .Execute(S) |
End With |
If matchs.Count = 0 Then |
RegGet = "" |
Else |
If m = 0 Then '取第一个值 |
RegGet = matchs(0).Value |
Else '取多个值 |
For Each e In matchs |
ss = ss & sp & e.Value |
Next |
RegGet = Mid(ss, Len(sp) + 1) |
End If |
End If |
Set regex = Nothing |
Set matchs = Nothing |
End Function |
Public Function RegTest(S, pString) '返回正则匹配\d[^_]*\d |
Dim matchs, regex |
Dim Temp, n |
Set regex = CreateObject( "VBScript.RegExp" ) |
With regex |
.Global = True |
.IgnoreCase = True |
.Pattern = pString |
RegTest = .test(S) |
End With |
Set regex = Nothing |
Set matchs = Nothing |
End Function |
Public Function RegSum(Rng As Range, Optional m = 0) '提取数字求和 |
Dim matchs, regex |
Set regex = CreateObject( "VBScript.RegExp" ) |
With regex |
.Global = True |
If m <> 0 Then |
.Pattern = "(\-|\+)?\d+(\.\d+)?" '//带小数点 |
Else |
.Pattern = "\d+" '//不带小数点 |
End If |
For Each e In Rng |
Set matchs = .Execute(e.Value) |
For Each Match In matchs |
RegSum = RegSum + Val(Match) |
Next |
Next |
End With |
Set regex = Nothing |
Set matchs = Nothing |
End Function |