excel中使用正则表达式提取单元格中匹配的字符
1、新建一个EXCEL表格

2、按ALT+F11打开VBA的代码窗口

3、鼠标右键点击项目,选择“插入”->“模块”

4、在新出的窗口中输入以下代码:
Function RegexMatch(cellValue As String, pattern As String)
Dim regex As Object
Dim matches As Object
Dim match As Object
Dim result As String
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.Pattern = pattern
If regex.test(cellValue) Then
Set matches = regex.Execute(cellValue)
For Each match In matches
result = result & match.Value & " "
Next match
RegexMatch = Trim(result)
Else
RegexMatch = "No match found"
End If
End Function

5、并将文件保存为 excel启用宏的工作薄

6、最后可以在单元格中输入=RegexMatch(A3,"\d+"),可以获取对应的内容

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
                                阅读量:32
阅读量:89
阅读量:172
阅读量:22
阅读量:178