2013-12-17 91 views
2

我需要聲明一個字符串用作正則表達式模式。Excel VBA正則表達式使用引號

該字符串是: (< = 「[A-ZA-Z0-9 .-] * \ d {8}的.xml(=?」)

一般聲明在VBA的字符串?在Reg Exp中使用時,請用雙引號括起來,使其看起來像這樣: 「(?< =」[a-zA-Z0-9 .-] * \ d {8} .xml(?=「)」 但導致VBA編譯錯誤:預期:以[a-zA-Z0-9.-]突出顯示的語句結束

This: 「(?< =」「」[a-zA-Z0- 9 .-] * \ d {8} .xml(?=「」「)」 導致相同的錯誤

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

但是當我使用Msgbox查看它顯示的模式是這樣的: 「?[A-ZA-Z0-9 .-] * \ d {8}的.xml(=」

(< = 「」)

,因此不會在正常工作!正則表達式

Arghhhh

下面是我使用的測試代碼:

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 

任何想法如何處理這個讚賞。

回答

2

嘗試做這樣的:

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

結果:

enter image description here

此外,VBA不支持回顧後,但它不支持先行。更好的參考可以發現here

+0

是。這通過了正確的模式。非常感謝。請注意,我在發佈的代碼中存在拼寫錯誤。我刪除了a)在所有的添加和刪除「試圖讓這個工作。應該有讀:(?<=」)[a-zA-Z0-9 .-] * \ d {8} .xml(? =「)。我遇到的第二個問題是這個字符串在Rubular(www.rubular.com)中工作,但不在VBA中,我得到運行時錯誤5017.但是,如果我使用我以前的模式(錯過了識別一些字符串I這是\ w +(?= - )( - )\ d {8}(。xml)。它的工作原理?想法?Excel VBA是否會排除向前和回溯命令? – mchac

+0

@mchac:編輯上述代碼以包含缺少''''另外,一定要檢查提供的關於lookaheads的鏈接,'VBScript'不支持lookbehind,這也適用於'VBA'. – Manhattan

+0

啊,對不起BK。我看到你的屏幕截圖並且被抽了 – mchac