我正在尝试使用正则表达式解析CSV文件。我打开文件并将其读入字符串。正则表达式字符串"([\ s \ S] *?)"网站http://www.regexr.com/上的匹配项。 locals窗口指出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)
答案 0 :(得分:1)
我假设您正在尝试匹配由双引号括起来的字符串。
你的模式不是你想象的那样。你的Chr's -
字符串&#34;(?\ S \ S *)&#34;
您错过了[ ]
我建议
.Pattern = """([\s\S]*?)"""
或者,或许:
.Pattern = Chr(34) & "([^" & Chr(34) & "]*)" & Chr(34)
OR
.Pattern = """([^""]*)"""
如果你必须使用Chr的字符串,那就是:
.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)