使用 Visual Basic for Applications 的正则表达式错误
Regular Expression error using Visual Basic for Applications
使用 http://www.regexr.com/ 模式进行测试 \"([\s\S]*?)\"
可以匹配我的目标字符串。但是,在我的代码中我得到了一个类型不匹配并且我不确定为什么,IDE 改变了字符串在引号
中的格式化方式
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)
你的引述有误。
字符串用引号引起来。要在字符串中包含引号,您需要 2 个引号。你的字符串开头有两个引号(所以是一个空字符串)你的字符串中间有一个单引号。如果字符串首先有效,则终止该字符串。
通常我们会像这样使用非懒惰的方式
.Pattern = Chr(34) & " \ " & Chr(34) & "(\s\S*?)\" & chr(34)
使用 http://www.regexr.com/ 模式进行测试 \"([\s\S]*?)\"
可以匹配我的目标字符串。但是,在我的代码中我得到了一个类型不匹配并且我不确定为什么,IDE 改变了字符串在引号
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)
你的引述有误。
字符串用引号引起来。要在字符串中包含引号,您需要 2 个引号。你的字符串开头有两个引号(所以是一个空字符串)你的字符串中间有一个单引号。如果字符串首先有效,则终止该字符串。
通常我们会像这样使用非懒惰的方式
.Pattern = Chr(34) & " \ " & Chr(34) & "(\s\S*?)\" & chr(34)