一、要让excel脚本支持Microsoft VBScript Regular Expressions 5.5 ,按快捷键alt+F11,出现下图界面,操作如图示:

二.添加VBA代码:

代码添加完毕后,关闭该窗口。

Function regex(strInput As String, matchPattern As String, Optional ByVal outputPattern As String = "$0") As Variant
Dim inputRegexObj As New VBScript_RegExp_55.RegExp, outputRegexObj As New VBScript_RegExp_55.RegExp, outReplaceRegexObj As New VBScript_RegExp_55.RegExp
Dim inputMatches As Object, replaceMatches As Object, replaceMatch As Object
Dim replaceNumber As Integer With inputRegexObj
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = matchPattern
End With
With outputRegexObj
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "\$(\d+)"
End With
With outReplaceRegexObj
.Global = True
.MultiLine = True
.IgnoreCase = False
End With Set inputMatches = inputRegexObj.Execute(strInput)
If inputMatches.Count = 0 Then
regex = False
Else
Set replaceMatches = outputRegexObj.Execute(outputPattern)
For Each replaceMatch In replaceMatches
replaceNumber = replaceMatch.SubMatches(0)
outReplaceRegexObj.Pattern = "\$" & replaceNumber If replaceNumber = 0 Then
outputPattern = outReplaceRegexObj.Replace(outputPattern, inputMatches(0).Value)
Else
If replaceNumber > inputMatches(0).SubMatches.Count Then
'regex = "A to high $ tag found. Largest allowed is $" & inputMatches(0).SubMatches.Count & "."
regex = CVErr(xlErrValue)
Exit Function
Else
outputPattern = outReplaceRegexObj.Replace(outputPattern, inputMatches(0).SubMatches(replaceNumber - 1))
End If
End If
Next
regex = outputPattern
End If
End Function

VBA脚本代码参数说明:

  1. A text to use the regular expression on.(第一个参数为被应用的字符串,即要从中提取的长字符串)
  2. A regular expression.(第二个参数为匹配的正则表达式,外侧需要加“”,支持捕获分组)
  3. A format string specifying how the result should look. It can contain $0$1$2, and so on. $0 is the entire match, $1 and up correspond to the respective match groups in the regular expression. Defaults to $0.(第三个参数为要捕获字符的字符分组,“$0”表示全部匹配捕获分组,“$1”,"$2"......表示捕获分组序号)

函数调用示例:

=regex("Peter Gordon: some@email.com, 47", "^(.+): (.+), (\d+)$", "$1")
结果:
Peter Gordon

  

  教程翻译自:https://stackoverflow.com/a/28176749/2109599

最新文章

  1. 【NLP】前戏:一起走进条件随机场(一)
  2. Linux下使用NMON监控、分析系统性能
  3. Java 16进制、unicode互转
  4. 11. 星际争霸之php设计模式--备忘模式
  5. js常用字符串方法汇总
  6. 原创: 做一款属于自己风格的音乐播放器 (HTML5的Audio新特性)
  7. HDU 4081 Qin Shi Huang's National Road System 最小生成树
  8. 一分钟明确 VS manifest 原理
  9. StarTeam SDK 13 下载安装
  10. JDBC之事务隔离级别以及ACID特性
  11. 【mysql注入】mysql注入点的技巧整合利用
  12. 吴恩达机器学习笔记3-代价函数II(cost function)
  13. 关闭VS2017脚本调试 已启用 Visual Studio 中的 Chrome 脚本调试
  14. python: 基本知识记录
  15. React的Element的创建和render
  16. Sql Server数据库之identity(自增)
  17. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS
  18. java 循环中使用list时,出现list中全部加入了对象导致没有实现分组的解决方案
  19. 路由表及route使用
  20. node.js的http模块创建基本Web服务器

热门文章

  1. win10如何查看文件扩展名
  2. android studio 模拟器无法联网的解决方法
  3. python的socke编程
  4. generator 生成器
  5. windows+Pycharm+Anaconda下安装opencv
  6. IDEA中使用中jetty启动java项目(非springboot)
  7. django 设置session过期时间
  8. PL/SQL简单使用——导入、导出数据表
  9. qsub
  10. Charles篡改后台数据