一、要让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

excel支持正则表达式提取字符函数(支持RegExp捕获分组)的更多相关文章

  1. 让bind函数支持IE8浏览器的方法

    bind函数在IE8下是不支持的,只需要在你的js文件中加入如下代码就可以支持IE8 //让bind函数支持IE8 if (!Function.prototype.bind) { Function.p ...

  2. ORACLE中的支持正则表达式的函数

    ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBS ...

  3. Oracle中REGEXP_SUBSTR及其它支持正则表达式的内置函数小结

    Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下:在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','23'的集合. REGEXP_SUBSTR函数格 ...

  4. SQL汉字转拼音函数-支持首字母、全拼

    SQL汉字转拼音函数-支持首字母.全拼 FROM :http://my.oschina.net/ind/blog/191659 作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 s ...

  5. linux(5)--补充(管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)

    本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下一. 管道| / 重定向> / xargs 如:1. 管道和重定向的区别:具体可以见 http://www. ...

  6. Excelize 2.4.0 正式版发布, 新增 152 项公式函数支持

    Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取.写入由 Microsoft Exc ...

  7. 字符截取 支持UTF8/GBK

    );         $n = $tn = $noc = ;          || $t ==  || ( <= $t && $t <= )) {             ...

  8. UTF-8、GB2312都支持的汉字截取函数

    <?php/*Utf-8.gb2312都支持的汉字截取函数cut_str(字符串, 截取长度, 开始长度, 编码);编码默认为 utf-8开始长度默认为 0*/ function cut_str ...

  9. ASCII字符集。扩展ASCII字符集。Unicode字符集分别支持多少个字符?

    ASCII字符集.扩展ASCII字符集.Unicode字符集分别支持多少个字符? 256个字符和 65536个字符

随机推荐

  1. QT项目添加现有文件后不能运行,MFC在类视图中自动隐藏类

    解决方案:1)QT 5.6版本的QtCreator打开pro文件,在最后加一行空行或者删除一行空行,保存即可: 2)在隐藏的类对应的头文件中增加一行或删除一行(空格也可以),即可自动出现.

  2. The Ultimate Productivity Hack is Saying No

    The Ultimate Productivity Hack is Saying No By James ClearRead this on JamesClear.com The ultimate p ...

  3. 用word发布CSDN文章

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  4. 如何使用wepy和 vant-weapp开发小程序

    这里记录一下  使用wepy框架和  vant-weapp库开发小程序废话 不多说 wepy文档: https://tencent.github.io/wepy/document.html#/ van ...

  5. 微信小程序生成携带参数的小程序码

    https://blog.csdn.net/qq_28988969/article/details/77969365

  6. cordova/ionic打包流程

    ionic android 手工打包指令手册 [工作平台要求] 安装 node.js.cordova.ionic(非必须) 等前端相关软件与组件(1)安装 Java-SDK,正确配置系统环境变量 JA ...

  7. IDEA 对比eclipse环境调节

    小子刚刚接触Intellij IDEA,以前用的都是eclipse.鉴于ieda的火热,开始学习之旅.本文会随时更新,记载idea中的一些调节方法,尽量在环境的配置上跟eclipse接近些. 在此感谢 ...

  8. rancher2.1.7 +jenkins +harbor 自动容器CI系统(通过rancher命令行)

    jenkins脚本执行示例: //环境定义与 cd $WORKSPACEmodule=news-usercd $module/case $deploy in deploy) //发布模块 //编译/o ...

  9. MMU二级页表

    https://blog.csdn.net/forDreamYue/article/details/78887035

  10. 第四周博客作业 <西北师范大学| 周安伟>

    一,助教博客链接https://home.cnblogs.com/u/zaw-315/ 二,本周点评作业量点评23份,对提交的优秀作业代码进行运行一份博客问题无法点评问题博客链接:https://ww ...