|
哈哈,你的想法与我的一致,用VBA自定义正则提取函数,自动判断是否存在捕获分组,如果有,则只提取捕获分组中的内容,如果没有,则提取匹配项的内容。其中,对于源字符串,可以是不定参数ParamArray Text1(),所以为函数的最后一项参数。所得结果为一维水平数组或单个字符串。
代码和在WPS中的运行效果如下:
- Function REGEXTRACT(ByVal Pattern$, ByVal Icase, ByVal Id, ByVal Slice, ParamArray Text1())
- Dim ar, i, j, tmp$(), n&, ma As Match, sma, itm, HasSubMatches As Boolean, mav As String
- If IsMissing(Icase) Then Icase = False Else Icase = CBool(Icase)
- If IsMissing(Id) Then Id = 0 Else Id = CLng(Id)
- If Id = 0 Then Slice = True Else If IsMissing(Slice) Then Slice = True Else Slice = CBool(Slice)
- With CreateObject("VBScript.RegExp")
- .Global = True
- .IgnoreCase = Icase
- .Pattern = Pattern & "|[\d\D]+"
- HasSubMatches = CBool(.Execute("a")(0).SubMatches.Count)
- .Pattern = Pattern
- ReDim tmp(1 To 1)
- For Each itm In Text1
- If TypeName(itm) = "Range" Then ar = itm.Value Else ar = itm
- If Not IsArray(ar) Then ReDim ar(1 To 1, 1 To 1): ar(1, 1) = itm
- For i = 1 To UBound(ar)
- For j = 1 To UBound(ar, 2)
- For Each ma In .Execute(ar(i, j))
- If HasSubMatches Then
- mav = ""
- For Each sma In ma.SubMatches
- mav = mav & sma
- Next
- Else
- mav = ma.Value
- End If
- If Len(mav) > 0 Then
- n = n + 1
- If Slice Or Id <= 0 Then
- ReDim Preserve tmp(1 To n)
- tmp(n) = mav
- Else
- tmp(1) = mav
- End If
- If n = Id Then GoTo ret
- End If
- Next
- Next
- Next
- Next
- End With
- ret:
- If Id < 0 And n >= -Id Then
- If Slice Then
- For i = 1 To -Id
- tmp(i) = tmp(n + Id + i)
- Next
- ReDim Preserve tmp(1 To -Id)
- Else
- tmp(1) = tmp(n + Id + 1)
- End If
- End If
- If Slice Then REGEXTRACT = tmp Else REGEXTRACT = tmp(1)
- End Function
复制代码
|
评分
-
1
查看全部评分
-
|