Find_and_Replace 和 RegExp 中正则表达式的差异

问题描述 投票:0回答:1

我需要查找并替换由以下字符组成的文本:* 一个字符,- 一个字符,一个或多个数字,- 一个字符,一个或多个字母,_ 一个或多个字符。

在 RegExp 中,我成功使用模式“*{1}-{1}\d{1,}-{1}[A-Z]{1,}_{1,}”来查找第一个匹配项(这在我的代码),然后我尝试在 Find_and_Replace 方法中应用相同的模式,但没有任何变化。

这是代码工作:

    Set regEx = New RegExp
    regEx.IgnoreCase = False
    regEx.MultiLine = True
    regEx.Global = False
    regEx.Pattern = "\*{1}\-{1}\d{1,}\-{1}[A-Z]{1,}\_{1,}"

    If regEx.Test(Rng) Then
        Set Matches = regEx.Execute(Rng)
        MsgBox Matches(0).FirstIndex + 1
    Else
        MsgBox 0
    End If

此代码不起作用:

    With Rng.Find
        .ClearFormatting
        .Text = "\*{1}\-{1}\d{1,}\-{1}[A-Z]{1,}\_{1,}"
        .Wrap = wdFindContinue
        .MatchWildcards = True
        With .Replacement
            .ClearFormatting
            .Font.Bold = True
            .Font.Underline = True
            
            .Text = "Bla-bla-bla"

        End With
        
        .Execute
    
        If .Found = True Then
            .Execute Replace:=wdReplaceOne
        End If
   end with
vba ms-word
1个回答
0
投票
.Text = "\*-[0-9]@-[A-Z]@_{1,}"
© www.soinside.com 2019 - 2024. All rights reserved.