|
看附件。
不过,如果说这个歪国人的正则定义函数称得上好用的话,那偶的这个就绝对是吊爆了。
前者只能从单个字符串中取得单个结果,我的可以从单个字符串中提取多个结果,还可以从多个单元格中提取多个结果并以数组形式返回,因此可以用于数组公式,而前者不能返回数组结果。
此外,前者使用了3个正则对象,而我的只用1个正则对象,对比系统开销,后者更优。前者需要执行额外的替换,我的直接用正则对象本身的替换功能,效率上后者更优。最后,从代码数量上看,我的更加简洁明了。
总之,你无我有,你有我强,你繁我简。
- Function REGREPLACE(Text1, ByVal Pttn$, Optional ByVal strReplacer$ = "", Optional ByVal Icase As Boolean = False)
- With CreateObject("VBScript.RegExp")
- .Global = True
- .IgnoreCase = Icase
- .Pattern = Pttn
- Dim ar, i, j
- If TypeName(Text1) = "Range" Then ar = Text1.Value Else ar = Text1
- If IsArray(ar) Then
- For i = 1 To UBound(ar)
- For j = 1 To UBound(ar, 2)
- If .Test(ar(i, j)) Then ar(i, j) = .Replace(ar(i, j), strReplacer) Else ar(i, j) = False
- Next
- Next
- Else
- If .Test(ar) Then ar = .Replace(ar, strReplacer) Else ar = False
- End If
- End With
- REGREPLACE = ar
- End Function
复制代码
正则自定义函数测试比较.rar
(20.78 KB, 下载次数: 0)
|
|