excel支持正则表达式提取字符函数(支持RegExp捕获分组)
一、要让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脚本代码参数说明:
- A text to use the regular expression on.(第一个参数为被应用的字符串,即要从中提取的长字符串)
- A regular expression.(第二个参数为匹配的正则表达式,外侧需要加“”,支持捕获分组)
- A format string specifying how the result should look. It can contain
$0,$1,$2, and so on.$0is the entire match,$1and 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捕获分组)的更多相关文章
- 让bind函数支持IE8浏览器的方法
bind函数在IE8下是不支持的,只需要在你的js文件中加入如下代码就可以支持IE8 //让bind函数支持IE8 if (!Function.prototype.bind) { Function.p ...
- ORACLE中的支持正则表达式的函数
ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBS ...
- Oracle中REGEXP_SUBSTR及其它支持正则表达式的内置函数小结
Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下:在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','23'的集合. REGEXP_SUBSTR函数格 ...
- SQL汉字转拼音函数-支持首字母、全拼
SQL汉字转拼音函数-支持首字母.全拼 FROM :http://my.oschina.net/ind/blog/191659 作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 s ...
- linux(5)--补充(管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)
本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下一. 管道| / 重定向> / xargs 如:1. 管道和重定向的区别:具体可以见 http://www. ...
- Excelize 2.4.0 正式版发布, 新增 152 项公式函数支持
Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取.写入由 Microsoft Exc ...
- 字符截取 支持UTF8/GBK
); $n = $tn = $noc = ; || $t == || ( <= $t && $t <= )) { ...
- UTF-8、GB2312都支持的汉字截取函数
<?php/*Utf-8.gb2312都支持的汉字截取函数cut_str(字符串, 截取长度, 开始长度, 编码);编码默认为 utf-8开始长度默认为 0*/ function cut_str ...
- ASCII字符集。扩展ASCII字符集。Unicode字符集分别支持多少个字符?
ASCII字符集.扩展ASCII字符集.Unicode字符集分别支持多少个字符? 256个字符和 65536个字符
随机推荐
- 一文让你秒懂互联网TCP/IP协议的深层含义
什么是 TCP/IP 协议 首先,协议,可以理解为是一套统一的规则,就像行业标准.由于互联网主要的功能是传输信息,所以其协议一般是管理系统之间如何相互通信的规则. 用邮政和物流等线下的“运输协议”来理 ...
- vue项目tips
在main.js引入了封装好的各组件,包括echarts.自定义组件等
- Windows 7升级1月更新汇总后导致SMBv2网络无法正常工作
在本月的补丁星期二活动日中,微软面向Windows 7.Windows 2008 R2服务器系统推出了KB4480970的月度更新汇总.然而根据部分用户反馈,在安装该更新之后导致系统的网络设置无法正常 ...
- JS应用猜数游戏
(function(){ let rs = require("readline-sync"); let guessNumber = new Set(); function getN ...
- html跳动的心实现代码
<style> .box{ width: 200px; height: 400px; positio ...
- display:flex布局
/*背景 居中 自适应 铺满容器*/ background: center / cover; flex布局是什么? flex是Flexibe Box 的缩写,意思为”弹性布局”, 用来为盒子模型提供最 ...
- 如何给php数组添加元素
以参考下 本文较为详细的总结了php数组添加元素方法.分享给大家供大家参考.具体分析如下: 如果我们是一维数组增加数组元素我们可以使用ArrayListay_push,当然除这种方法之外我们还有更直接 ...
- eleTree树形插件引入
先放一个效果 @eleTree插件官网 刚开始用的xtree,不知为何总是达不到效果,不得已从layui官网插件中寻得这个eleTree插件,看着还不错,用法也简单.不过还是有点坑需要大家注意 开始使 ...
- spring mvc 映射与适配器
在深入学习Spring mvc 过程中,我们需要了解如下两个类: org.springframework.web.servlet.mvc.method.annotation.RequestMappin ...
- anaconda使用方法
我是用的win10.想写爬虫,所以使用了 anaconda .总结一下使用的方法. 安装阶段跳过.: 再终端输入 jupyter notebook 然后就会在. 浏览器显示一个网页.其实这个也不 ...