正则表达式出错

晃仙 2019-05-29 03:39:22
想处理数据成图片里的样子,正则表达式 myreg2.Pattern = "\s*(\S+)奖(\S+)” myreg2.Pattern = "(\w+)奖(\w+)" 比如 出勤奖三等 销售奖二等 excel运行上面对下面错,下面的为空值,可是正则测试器里两个都是对的,这是为什么? Sub demo3() Worksheets(1).Activate Dim myreg1 As Object, myreg2 As Object, mymatch1 As Object Dim mymc, mymatch2 As Object, k, mymatches1, mymatches2, mydata, i, j i = 6 Set myreg1 = CreateObject("vbscript.regexp") myreg1.Global = True myreg1.Pattern = "姓名:\s*(\S+)\s*获奖情况:([^;]+);" Set myreg2 = CreateObject("vbscript.regexp") myreg2.Global = True myreg2.Pattern = "\s*(\S+)奖(\S+)" 'myreg2.Pattern = "(\w+)奖(\w+)" mydata = Cells(1, 1).Value Set mymatch1 = myreg1.Execute(mydata) For Each mymatches1 In mymatch1 mymc = mymatches1.submatches(1) Set mymatch2 = myreg2.Execute(mymc) For Each mymatches2 In mymatch2 MsgBox mymatches2.submatches(0) Next Next
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2019-06-02
  • 打赏
  • 举报
回复
引用 1 楼 milaoshu1020 的回复:
\w 匹配字母或数字或下划线或汉字 等价于 '[^A-Za-z0-9_]'。 可能是 \w 不能匹配中文的原因;
勤勤恳恳米老鼠,为啥不来当版主,VB板块太荒芜,急需杰瑞来帮助
milaoshu1020 2019-05-29
  • 打赏
  • 举报
回复
刚才我转的内容不对:
引用 1 楼 milaoshu1020 的回复:
\w 匹配字母或数字或下划线或汉字 等价于 '[^A-Za-z0-9_]'。


注意:
\w能不能匹配汉字要视你的操作系统和你的应用环境而定;
可能是EXCEL VBA中 \w 不能匹配中文的原因;
milaoshu1020 2019-05-29
  • 打赏
  • 举报
回复
\w 匹配字母或数字或下划线或汉字 等价于 '[^A-Za-z0-9_]'。

可能是 \w 不能匹配中文的原因;

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧