当前位置:主页 > Office办公 > excel正则表达式函数

excel正则表达式函数

excel 中如何使用正则表达式函数提取或替换内容
excel 中如何使用正则表达式函数提取或替换内容

word中自带正则表达式,不过Excel中却没有内置。可以在VBA中定义一个正则表达式函数,然后Excel中就可以调用了。VBA代码如下(VBA高亮无效,凑合着看):1.Function ExStr(Str As String, Parttern As String, ActionID As Integer, Optional RepStr As String = "") 2. Dim regex As Object 3. Set regex = CreateObject("vbscript.regexp") 4. With regex 5. .Global = True 6. .IgnoreCase = True 7. .MultiLine = True 8. .Pattern = Parttern 9. End With 10. Select Case ActionID 11. Case 1: '替换 12. ExStr = regex.Replace(Str, RepStr) 13. Case 2: '判断 14. ExStr = regex.test(Str) 15. Case 3: '提取 16. Dim matches As Object 17. Set matches = regex.Execute(Str) 18. For Each Match In matches 19. ExStr = ExStr & Match.Value 20. Next 21. End Select 22.End Function'正则表达式 V2'增加:完善注释'函数ExStr功能,根据正则表达式,替换或判断或提取字符串'参数 str 源字符串'参数 Parttern 正则表达式字符串'参数 ActionID'函数功能'1正则方法一,替换符合正则表达式的字符,可选参数RepStr为替换值

1751 次浏览