在Excel中使用VBA的正则expression式不符合预期

我正在尝试使用正则expression式parsingCSV文件。 我打开文件并将其读入一个string。 正则expression式string“([\ s \ S] *?)”与网站http://www.regexr.com/匹配。 本地窗口声明FirstIndex项目在位置172.文件中的第一个字符由“插件”,“插件名称”组成。 不知道我错过了什么?

Dim Matches Dim objectRegularExp As RegExp Dim Match 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) Set objectRegularExp = New RegExp With objectRegularExp ' "([\s\S]*?)" .Pattern = Chr(34) & Chr(40) & Chr(92) & Chr(115) & Chr(92) & Chr(83) & Chr(42) & Chr(63) & Chr(41) & Chr(34) .Global = True .MultiLine = True End With Set Matches = objectRegularExp.Execute(yourText) 

我从你的模式中假设你正在试图匹配由双引号括起来的string。

你的模式不是你想象的那样。 你的Chr串 –

“(\ S \ S *?)”

你错过了[ ]

我会build议

 .Pattern = """([\s\S]*?)""" 

也许:

  .Pattern = Chr(34) & "([^" & Chr(34) & "]*)" & Chr(34) 

要么

 .Pattern = """([^""]*)""" 

如果你必须使用Chr的string,那将是:

 .Pattern = Chr(34) & Chr(40) & Chr(91) & Chr(92) & Chr(115) & Chr(92) & Chr(83) & Chr(93) & Chr(42) & Chr(63) & Chr(41) & Chr(34)