一、要让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. JavaFX(Maven 方式)

    运行界面第一种方式 运行界面第二种方式 参考文献 https://github.com/AlmasB/JavaFX11-example

  2. kinect 深度图与彩色图对齐程序

    //#include "duiqi.hpp" #include "kinect.h" #include <iostream> #include &q ...

  3. H5自定义金额键盘,改良后ios体验效果流畅

    下载的别人的插件改良一下,源码地址:https://github.com/XieTongXue/how-to/tree/master/pay-h5 没有插件,直接来代码 <div class=& ...

  4. celery (二) task

    Task task 具有如下特点: task 可以在任何可调用的地方创建.它有双重角色: 定义了当task被调用时,会发送一个消息. 定义了当worker收到消息时会运行消息对应的函数 每个task都 ...

  5. UI测试和GUI测试的区别

    UI 测试 包含GUI测试和command line 测试 分享连接 https://www.ranorex.com/resources/testing-wiki/gui-testing/

  6. Git自学笔记

    Git是什么? Git是目前世界上最先进的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git与SVN的区别有哪些? ① Git是分布式的,SVN不是.这是Git和其它非分布式版本控制系 ...

  7. 3D Math Keynote 4

    [3D Math Keynote 4] 1.三角带. 合并三角带能够提升渲染效率. 三角扇. 2.边缩坍,将边缩减为顶点 . 网格消减,使用边缩坍,可以实现渐进式网络. 3.下图左边是面拆分.右边是焊 ...

  8. woff/woff2字体404找不到

    每次控制台都报这个错,很纳闷,服务器上明明放了字体文件,怎么找不到呢 今天突然想起来,IIS的MIME类型要配置一下 之前部署网站,一个链接下载app的时候,IIS就不识别apk格式的文件,尽管服务器 ...

  9. 国内安装helm

    helm repo remove stable helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts ...

  10. Ionic2+WebApi 导出Excel转Pdf文件。

    步骤: 1.首先在WebApi中先把excel生成好. 2.把excel转成Pdf,并返回下载的链接. 3.Ionic2的页面做好下载的接口. 嗯~思路很清晰,那么下面就来详细的操作吧. 以下是H5的 ...