gpt4 book ai didi

regex - Excel VBA 正则表达式使用引号

转载 作者:行者123 更新时间:2023-12-02 18:34:02 24 4
gpt4 key购买 nike

我需要声明一个字符串用作正则表达式模式。

字符串是:(?<="[a-zA-Z0-9.-]*\d{8}.xml(?=")

通常要在 VBA 中声明一个字符串以便在 Reg Exp 中使用,您需要用双引号引起来,因此它看起来像这样:"(?<="[a-zA-Z0-9.-]*\d{8}.xml(?=")"但这会导致 VBA 编译错误:预期:语句结束,并突出显示 [a-zA-Z0-9.-]。

这个:"(?<="""[a-zA-Z0-9.-]*\d{8}.xml(?=""")"导致同样的错误。

这个"(?<=""""[a-zA-Z0-9.-]*\d{8}.xml(?="""")"

可以工作,但是当我使用 Msgbox 查看模式时,它看起来像这样:

(?<=""[a-zA-Z0-9.-]*\d{8}.xml(?="")

因此在正则表达式中无法正常工作。

啊啊啊!

这是我用于测试的代码:

    Sub tester()
Dim PATH_TO_FILINGS As String
'PATH_TO_FILINGS = "www.sec.gov/Archives/edgar/data/1084869/000110465913082760"
PATH_TO_FILINGS = "www.sec.gov/Archives/edgar/data/1446896/000144689612000023"
MsgBox GetInstanceDocumentPath(PATH_TO_FILINGS)
End Sub

Function GetInstanceDocumentPath(PATH_TO_FILINGS As String)

'this part launches IE and goes to the correct directory
If IEbrowser Is Nothing Then
Set IEbrowser = CreateObject("InternetExplorer.application")
IEbrowser.Visible = False
End If

IEbrowser.Navigate URL:=PATH_TO_FILINGS

While IEbrowser.Busy Or IEbrowser.readyState <> 4: DoEvents: Wend

'this part starts the regular expression engine and searches for the reg exp pattern (i.e. the file name)
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.Pattern = "(?<="[a-zA-Z0-9.-]*\d{8}.xml(?=")" '"\w+(?=-)(-)\d{8}(.xml)"
MsgBox RE.Pattern
RE.IgnoreCase = True

Dim INSTANCEDOCUMENT As Object

Set INSTANCEDOCUMENT = RE.Execute(IEbrowser.Document.body.innerhtml)

If INSTANCEDOCUMENT.Count = 1 Then

GetInstanceDocumentPath = PATH_TO_FILINGS & "/" & INSTANCEDOCUMENT.Item(0)

End If

End Function

任何关于如何解决这个问题的想法都值得赞赏。

最佳答案

尝试这样做:

Sub Test()
RealQ = Chr(34)
Pattern = "(?<=" & RealQ & ")[a-zA-Z0-9.-]*\d{8}.xml(?=" & RealQ & ")"
MsgBox Pattern
End Sub

结果:

enter image description here

此外,VBA 不支持lookbehind,但它支持lookahead。更好的引用可以找到here .

关于regex - Excel VBA 正则表达式使用引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20624978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com