无法在 vba IE 中应用正则表达式
Unable to apply regex within vba IE
我已经使用 vba 结合 IE 编写了一个脚本来解析应用 regex[=31= 的网页中的联系信息] 在上面。我搜索了很多但找不到任何可以满足我要求的例子。 pattern
可能不是找到 phone
号码的理想选择,但这里主要关心的是如何在 vba IE.
中使用 pattern
再一次:我的目的是在 vba IE 中应用 regex
从该网页解析 phone 数字 661-421-5861
。
这是我目前尝试过的方法:
Sub FetchItems()
Const URL$ = "https://www.nafe.com/bakersfield-nafe-network"
Dim IE As New InternetExplorer, HTML As HTMLDocument
Dim rxp As New RegExp, email As Object, Row&
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
End With
With rxp
.Pattern = "(?<=Phone:)\s*?.*?([^\s]+)"
Set email = .Execute(HTML.body.innerText) 'I'm getting here an error
If email.Count > 0 Then
Row = Row + 1: Cells(Row, 1) = email.Item(0)
End If
End With
IE.Quit
End Sub
当我执行上面的脚本时遇到错误 method "Execute" of object "IRegExp2" failed when它命中包含 Set email = .Execute(HTML.body.innerText)
的行。我怎样才能让它成功?
请注意,VBA 正则表达式不支持回顾。在这里,您可能希望在 Phone:
.
之后捕获任意数字后跟任意数量的数字和连字符
您需要将模式重新定义为
rxp.Pattern = "Phone:\s*(\d[-\d]+)"
然后,您需要获取第一个匹配项并访问其 .SubMatches(0)
:
Set email = .Execute(HTML.body.innerText)
If email.Count > 0 Then
Cells(Row+1, 1) = email.Item(0).SubMatches(0)
End If
见regex in action。 sting 的绿色突出显示部分是 .SubMatches(0)
持有的内容。
图案详情
Phone:
- 文字子串
\s*
- 0+ 个空格
(\d[-\d]+)
- 捕获第 1 组:一个数字,后跟 1+(由于 +
,您可以用 *
替换以匹配零个或多个)数字 or/and连字符。
这是使用 xmlhttp 对象的更快捷的方法
Sub FetchItems()
Dim URL As String, strBody As String
Dim intS As Long, intE As Long
URL = "https://www.nafe.com/bakersfield-nafe-network"
Dim xml As Object
Set xml = CreateObject("MSXML2.XMLHTTP")
xml.Open "GET", URL, False
xml.send
Dim html As Object
Set html = CreateObject("htmlfile")
html.body.innerHTML = xml.responseText
strBody = html.body.innerHTML
intS = InStr(1, strBody, "Phone:", vbTextCompare) + Len("Phone:")
intE = InStr(intS, strBody, "<", vbTextCompare)
MsgBox Mid(strBody, intS, intE - intS)
End Sub
我已经使用 vba 结合 IE 编写了一个脚本来解析应用 regex[=31= 的网页中的联系信息] 在上面。我搜索了很多但找不到任何可以满足我要求的例子。 pattern
可能不是找到 phone
号码的理想选择,但这里主要关心的是如何在 vba IE.
pattern
再一次:我的目的是在 vba IE 中应用 regex
从该网页解析 phone 数字 661-421-5861
。
这是我目前尝试过的方法:
Sub FetchItems()
Const URL$ = "https://www.nafe.com/bakersfield-nafe-network"
Dim IE As New InternetExplorer, HTML As HTMLDocument
Dim rxp As New RegExp, email As Object, Row&
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
End With
With rxp
.Pattern = "(?<=Phone:)\s*?.*?([^\s]+)"
Set email = .Execute(HTML.body.innerText) 'I'm getting here an error
If email.Count > 0 Then
Row = Row + 1: Cells(Row, 1) = email.Item(0)
End If
End With
IE.Quit
End Sub
当我执行上面的脚本时遇到错误 method "Execute" of object "IRegExp2" failed when它命中包含 Set email = .Execute(HTML.body.innerText)
的行。我怎样才能让它成功?
请注意,VBA 正则表达式不支持回顾。在这里,您可能希望在 Phone:
.
您需要将模式重新定义为
rxp.Pattern = "Phone:\s*(\d[-\d]+)"
然后,您需要获取第一个匹配项并访问其 .SubMatches(0)
:
Set email = .Execute(HTML.body.innerText)
If email.Count > 0 Then
Cells(Row+1, 1) = email.Item(0).SubMatches(0)
End If
见regex in action。 sting 的绿色突出显示部分是 .SubMatches(0)
持有的内容。
图案详情
Phone:
- 文字子串\s*
- 0+ 个空格(\d[-\d]+)
- 捕获第 1 组:一个数字,后跟 1+(由于+
,您可以用*
替换以匹配零个或多个)数字 or/and连字符。
这是使用 xmlhttp 对象的更快捷的方法
Sub FetchItems()
Dim URL As String, strBody As String
Dim intS As Long, intE As Long
URL = "https://www.nafe.com/bakersfield-nafe-network"
Dim xml As Object
Set xml = CreateObject("MSXML2.XMLHTTP")
xml.Open "GET", URL, False
xml.send
Dim html As Object
Set html = CreateObject("htmlfile")
html.body.innerHTML = xml.responseText
strBody = html.body.innerHTML
intS = InStr(1, strBody, "Phone:", vbTextCompare) + Len("Phone:")
intE = InStr(intS, strBody, "<", vbTextCompare)
MsgBox Mid(strBody, intS, intE - intS)
End Sub