在Word文档中搜索文本(单引号/双引号)



我正在尝试获取(单引号/双引号(中存在的数据列表

输入数据为:

维问拉杰"你好吗?"和"你去哪儿了"。拉杰回答道 "我很好,拉维,'你好吗'和'你怎么认识这个人?'">

预期输出为:

  • 你好吗
  • 你去哪了
  • 我很好,拉维,"你好吗"和"你怎么认识这个人?

(需要考虑边界引号,并且内部应嵌入单/双引号字符(

请在Word VBA中提供有关RegExp的建议。

我正在尝试下面的代码,但没有成功:

Sub Test()
Dim mystring As RegExp
Dim mydata As MatchCollection
Set mystring = New RegExp
mystring.Pattern = "s("".*"")s"
mystring.Global = True
Set mydata = mystring.Execute(ActiveDocument.Range)

For Each wrd In mydata
MsgBox wrd
Next wrd
End Sub  

这应该有效:

".+?"|'.+?'

在线试用。

但是,这包括匹配项中的单引号/双引号,这似乎不是您的预期输出。不过,您可以使用VBA删除它们。

一个完整的VBA示例:

Sub Test()
Dim re As RegExp
Dim matches As MatchCollection
Dim m As Match
Set re = New RegExp
re.Pattern = """.+?""|'.+?'"
re.Global = True
Set matches = re.Execute(ActiveDocument.Range)
For Each m In matches
'MsgBox m           ' With quotes.
Dim parsed As String
parsed = Mid$(m, 2, Len(m) - 2)
MsgBox parsed       ' Without quotes.
Next m
End Sub

这个似乎有效:

(?:'|").*(?:'|")

((?:'|").*(?:'|"))

如果你需要一个小组。

这是演示:链接

它有效,因为*是一个贪婪的量词,所以你不必知道最终是什么样的报价。 * 将尽可能多地使用。

最新更新