注册 登录
编程论坛 Excel/VBA论坛

正则表达式找不出匹配内容,求解。

山泉水 发布于 2023-06-01 15:26, 512 次点击
本人写了代码,找不出问题。请各位大神帮忙解决,为什么正则匹配不到合适字符串。
目标字符串s内容简单:"快递费21200办公费8120"
代码如下,虽然有问题,自己查不出来。请大神帮忙检查。
感谢!
Option Explicit

Sub regexpDemo()

Dim s$, i&

Dim myReg As Object

Dim myMatches As Object, myMatch As Object

i = 1

s = ""

Do While Cells(i, 1) <> ""

    s = s & Cells(i, 1).Value

    i = i + 1

Loop

Set myReg = CreateObject("vbscript.regexp")

myReg.Pattern = "\s*(\w+费)(\d+)\s*"

myReg.Global = True

Set myMatches = myReg.Execute(s)

MsgBox myMatches.Count

i = 1

For Each myMatch In myMatches

    Cells(i, 2) = myMatch.submatches(0)
   
    Cells(i, 3) = myMatch.submatches(1)
   
    i = i + 1

Next myMatch

End Sub
只有本站会员才能查看附件,请 登录

3 回复
#2
山泉水2023-06-01 15:32
用正则表达式测试软件可以找出子字符串,但是VBA中找不出。请大神帮忙看下,谢谢!
只有本站会员才能查看附件,请 登录
#3
阳光上的桥2023-06-02 13:03
这样写:


myReg.Pattern = "\s*(\S+?费)(\d+)\s*"
#4
阳光上的桥2023-06-02 13:04
myReg.Pattern = "\s*(.+?费)(\d+)\s*"
1