3

我正在尝试使用 VBA 中的正则表达式从 XML 中提取一些数据,方法是匹配元素的开始打开和结束标记,但我什么也没得到。

我可以<foo>.+?<\/foo>在 Notepad++ 中使用,但它不能在带有 Microsoft 正则表达式 5.5 的 VBA 中使用

<foo>
variable data here 
-
-
</foo>
4

2 回答 2

2

这是因为 . 在 VBA 中不包括换行符。您可以在您的模式定义中使用 (.|\n)* 来包含换行符 \n 对于您的示例<foo>(.|\n)*<\/foo> 您也可以使用<foo>[^<]*<\/foo>if 在 foo 块之间您不期望 <

于 2019-10-18T08:43:07.947 回答
1

这是列出所有内容的示例<td>

Sub MatchXMLtags()
  Dim xml As String
  xml = "<td>a</td><td>b" & vbCrLf & "</td><td>c</td>" & vbCrLf & "<td>d</td>"

  Dim match As Object
  With CreateObject("VBScript.RegExp")
    .pattern = "<td>\s*([\S\s]+?)\s*</td>"
    .Global = True
    .IgnoreCase = True
    .MultiLine = False

    ' display the content of each td tag
    For Each match In .Execute(xml)
      Debug.Print match.SubMatches(0)
    Next
  End With
End Sub
于 2016-04-03T16:08:15.993 回答