在 VBA 中结合正则表达式和用户输入

Combining Regex and User Input in VBA

我试图让用户搜索字符串数组。我希望我的搜索模式是:

以下表达式应匹配:

  1. "ap" 到 "apple"
  2. “嗨”到"hi"

在 PowerPoint VBA 中,将用户输入与星号组合时出现语法错误,如下所示:

Sub RegEx_Tester()


Set objRegExp_1 = CreateObject("vbscript.regexp")

objRegExp_1.Global = True
objRegExp_1.IgnoreCase = True

Dim test As String
    test = InputBox("Give a Section or Presentation")
    strToSearch = test
    objRegExp_1.Pattern = \s*[test]*


    Set regExp_Matches = objRegExp_1.Execute(strToSearch)

    If regExp_Matches.Count = 1 Then
    'Add it to another array. Unimportant for this question.
    End If

End Sub

我的正则表达式模式以红色突出显示,并且出现语法错误。我的问题是:

不能将变量和正则表达式结合起来吗?还是我只是搞砸了语法?

您需要将字符串放在引号中:

"\s*[test]*"

但这也不完全正确。这将匹配字母 t、e 或 s。 您可能会查看 TRIM( text ) ,它删除了前导空格和尾随空格。我包含的 link 看起来只适用于 Word 和 Excel,但 Powerpoint 可能有类似的东西。