|
楼主 |
发表于 2014-1-14 13:31
|
显示全部楼层
本帖最后由 香川群子 于 2014-1-15 23:09 编辑
本帖虽然只是面向初学者用的正则练习用自定义函数,但功能多、调试方便,非常具有实用性。
建议立即收录到知识树中去……
- Function TQ(txt$, Optional k = 0, Optional pt = 1, Optional s$ = "")
- If IsNumeric(pt) Then pt = Choose(pt, "\w", "[^a-zA-Z]", "\D", "[^a-z]", "[^A-Z]", "\W", "\d")
- With CreateObject("VBScript.RegExp")
- .Global = True
- .Pattern = pt
- If .test(txt) Then
- If k = 0 Then
- TQ = .Replace(txt, s)
- ElseIf k > 0 Then
- If InStr(k, ".") Then
- Set Ma = .Execute(txt)
- ReDim a(0 To Ma.Count - 1)
- For Each m In Ma
- a(c) = m: c = c + 1
- Next
- If s = "" Then s = " "
- TQ = Join(a, s)
- Else
- TQ = .Execute(txt)(k - 1)
- End If
- Else 'k < 0
- If InStr(k, ".") Then
- TQ = .Execute(txt)(Int(-k) - 1).SubMatches(Mid(k, InStr(k, ".") + 1) - 1)
- Else
- Set sMa = .Execute(txt)(-k - 1).SubMatches
- ReDim b(0 To sMa.Count - 1)
- For Each m In sMa
- b(c) = m: c = c + 1
- Next
- If s = "" Then s = " "
- TQ = Join(b, s)
- End If
- End If
- Else
- If k = 0 And s = "" Then TQ = txt Else TQ = ""
- End If
- End With
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|