EXCEL 定义正则函数,指定分隔符返回所有匹配内容

该文章介绍了一个名为REGEXP的自定义函数,它利用VBScript的正则表达式对象来从文本中提取匹配的内容。函数接受三个参数:待处理文本、正则表达式模式和可选的分隔符。通过对所有匹配项及其子匹配进行迭代,将结果连接并用分隔符分隔。最后,如果结果字符串以分隔符开头,会去除多余的分隔符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数

Function REGEXP(ByVal text As String, _
                      ByVal extract_what As String, _
                      Optional seperator As String = "") As String

Dim i As Long, j As Long
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.Pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)

For i = 0 To allMatches.Count - 1
    For j = 0 To allMatches.Item(i).submatches.Count - 1
        result = result & seperator & allMatches.Item(i).submatches.Item(j)
    Next
Next

If Len(result) <> 0 Then
    result = Right(result, Len(result) - Len(seperator))
End If

REGEXP = result

End Function

调用

# =REGEXP(单元格,"正则表达式",",分隔符")
=REGEXP(E2,"(<.*?>)",",")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值