使用Visual Basic for Applications的正则expression式错误

使用http://www.regexr.com/模式\"([\s\S]*?)\"可以匹配我的目标string。 然而,在我的代码中,我得到一个types不匹配,不知道为什么,IDE改变了string格式在引号

 Dim Matches Dim objectRegularExp As RegExp If Application.FileDialog(msoFileDialogOpen).Show <> -1 Then Exit Sub FileName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1) Open FileName For Input As #1 yourText = Input(LOF(1), #1) Close #1 LengthOfFile = Len(yourText) 'create regular expression Set objectRegularExp = New RegExp With objectRegularExp .Pattern = "" \ "(\s\S*?)\""" <type mismatch runtime error .Global = True .MultiLine = True End With Set Matches = objectRegularExp.Execute(yourText) 

你的报价是错误的。

string用引号引起来。 要用string括起来,你需要两个引号。 你有两个quotesat你的string开头(这样一个空string)你有一个单引号在你的string中间。 如果它是有效的,则会终止string。

通常我们使用非懒惰的方式如此

.Pattern = Chr(34)&“\”&Chr(34)&“(\ s \ S *?)\”&chr(34)