ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 7658|回复: 8

[转帖] 超级好用的一个正则表达式自定义函数

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-5-26 20:58 | 显示全部楼层 |阅读模式
本帖最后由 蝶梦楼主 于 2018-5-26 21:06 编辑

超级好用的一个正则表达式自定义函数

- 转译自:<https://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops>

使用说明:

- 如何使用自定义函数请自行搜索...


<p><strong>操作符表:</strong></p>
Order Name Representation

---------

1 成组 ( )

2 成群 ? + * {m,n} {m, n}?

3 锚点 abc ^ $

4

5 取组$0-$9

[\table]

**字符表:**

[table]

abr same as meaning

---------

\d [0-9] 数字

\D [^0-9] 非数字

\w [a-zA-Z0-9_] 数字和字母

\W [^a-zA-Z0-9_] 非数字字母

\s [ \r\t
\f]
空白

\S [^ \r\t
\f]
非空白


[
]
新行


几个例子:
> =regex("Peter Gordon: some@email.com, 47", "\w+@\w+\.\w+")

> =regex("Peter Gordon: some@email.com, 47", "\w+@\w+\.\w+", "$0")

> Results in: some@email.com

> =regex("Peter Gordon: some@email.com, 47", "^(.+): (.+), (\d+)$", "E-Mail: $2, Name: $1")

> Results in: E-Mail: some@email.com, Name: Peter Gordon

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

> =regex("Peter Gordon: some@email.com, 47", "^(.+): (.+), (\d+)$", "$" & 2)

> Results in: Peter Gordon some@email.com .
  1. Function regex(strInput As String, matchPattern As String, Optional ByVal outputPattern As String = "$0") As Variant
  2.     Dim inputRegexObj As New VBScript_RegExp_55.RegExp, outputRegexObj As New VBScript_RegExp_55.RegExp, outReplaceRegexObj As New VBScript_RegExp_55.RegExp
  3.     Dim inputMatches As Object, replaceMatches As Object, replaceMatch As Object
  4.     Dim replaceNumber As Integer

  5.     With inputRegexObj
  6.         .Global = True
  7.         .MultiLine = True
  8.         .IgnoreCase = False
  9.         .Pattern = matchPattern
  10.     End With
  11.     With outputRegexObj
  12.         .Global = True
  13.         .MultiLine = True
  14.         .IgnoreCase = False
  15.         .Pattern = "\$(\d+)"
  16.     End With
  17.     With outReplaceRegexObj
  18.         .Global = True
  19.         .MultiLine = True
  20.         .IgnoreCase = False
  21.     End With

  22.     Set inputMatches = inputRegexObj.Execute(strInput)
  23.     If inputMatches.count = 0 Then
  24.         regex = False
  25.     Else
  26.         Set replaceMatches = outputRegexObj.Execute(outputPattern)
  27.         For Each replaceMatch In replaceMatches
  28.             replaceNumber = replaceMatch.SubMatches(0)
  29.             outReplaceRegexObj.Pattern = "\$" & replaceNumber

  30.             If replaceNumber = 0 Then
  31.                 outputPattern = outReplaceRegexObj.Replace(outputPattern, inputMatches(0).Value)
  32.             Else
  33.                 If replaceNumber > inputMatches(0).SubMatches.count Then
  34.                     'regex = "A to high $ tag found. Largest allowed is $" & inputMatches(0).SubMatches.Count & "."
  35.                     regex = CVErr(xlErrValue)
  36.                     Exit Function
  37.                 Else
  38.                     outputPattern = outReplaceRegexObj.Replace(outputPattern, inputMatches(0).SubMatches(replaceNumber - 1))
  39.                 End If
  40.             End If
  41.         Next
  42.         regex = outputPattern
  43.     End If
  44. End Function
复制代码




评分

2

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-5-27 08:46 来自手机 | 显示全部楼层
插入一个module,粘进去上面的代码,工具中引用"Microsoft VBScript Regular Expressions 5.5" 就OK了。

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-5-27 08:55 来自手机 | 显示全部楼层
也可以新建一个xlsm文件,在加载项中引用,这样就可以在所有xl文件中使用了

TA的精华主题

TA的得分主题

发表于 2018-5-27 11:57 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
正则 相当于一门外语。

TA的精华主题

TA的得分主题

发表于 2018-5-28 13:45 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2018-5-28 14:14 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-3-2 16:32 | 显示全部楼层
例子都是提取单个连续字符的。如果提取多个不连续字符串,也能满足吗?例如,从以下手机短信中提取日期:
str1="您的尾号为xxx的账号于19年3月12日支出800.12元"
str2="您的尾号为xxx的账号于18年11月2日支出800.12元"
提取日期结果分别为:19-3-12,18-11-2

TA的精华主题

TA的得分主题

发表于 2019-3-24 21:55 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-3-31 21:30 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我需要的,好好学习学习。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-25 04:53 , Processed in 0.038164 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表