文本提取处理领域,正则表达式是最为强大的存在,工作中Excel 是常用的小型数据采集,处理,分析的工具但本身不具备正则的能力,让Excel拥有正则的能力无疑是如虎添翼的能力。
方案
让正则作为函数内容的一部分,给这类业务正则直接命名一个函数名称,调用时直接访问和Excel 系统函数一样简单
函数自定义
vbnet
RegexExtract(rng As Range, ByVal Pattern As String, Optional MatchIndex As Integer = 0, Optional IgnoreCase As Boolean = True)
参数名称 | 简介 |
---|---|
rng | 需要提取文本的单元格 |
Pattern | 正则表达式规则字符串,备注:用户根据场景自定义,比如\d+,表示提取连续字符串 |
MatchIndex | 单元格中匹配成立的字符串集合中,取第几个,起始索引为 0,默认为 0 |
IgnoreCase | 匹配规则属性设置,是否区分大小写,True 区分,False 不区分 |
Function Code
vbnet
' 提取字符串中符合正则规则的内容
Function RegexExtract(rng As Range, ByVal Pattern As String, Optional MatchIndex As Integer = 0, Optional IgnoreCase As Boolean = True) As String
' on error resume next
Dim regEx As Object, matches As Object
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.Pattern = Pattern
.IgnoreCase = IgnoreCase
.Global = True
End With
Set matches = regEx.Execute(rng.Value)
If matches.Count > 0 Then
If MatchIndex < 0 Or MatchIndex >= matches.Count Then
RegexExtract = "Index out of range"
Else
RegexExtract = matches(MatchIndex).Value
End If
Else
RegexExtract = "No match"
End If
Set regEx = Nothing
End Function
代码运行过程演示
