首页 > 解决方案 > 需要使用 html 对正则表达式进行更精细的调整

问题描述

我想做的是弄清楚如何使用正则表达式从 HTML 标记集合中提取最里面的项目。即:目标文本

Function FindInnerHtml(Work As String) As String
Dim Results As String, myRegExp, myMatches As Object, thisMatch As Object
  Let myRegExp = New RegExp
  myRegExp.IgnoreCase = True
  myRegExp.Global = True
  myRegExp.Pattern = ">(.*?)<"
  Set myMatches = myRegExp.Execute(Work)
  If (myMatches.Count = 0) Then
    Results = myMatches(0)
    Results = Replace$(Replace$(Results, ">", ""), "<", "")
  End If
  FindInnerHtml = Results
End Function

我从函数中得到的是内部 HTML,即目标文本,我宁愿能够做的是确保我不需要添加双 replace$() 来清理结果。

标签: regexvba

解决方案


对于边缘情况,它很粗糙并且惨遭失败,但这样的事情可能会奏效:

<[a-zA-Z]{1}[a-zA-Z\d]*>([^><]*)</[a-zA-Z]{1}[a-zA-Z\d]*>

$1将包含内部文本

https://regex101.com/r/iuLdJV/3


推荐阅读