2016-06-08 347 views
2

完全字符串:嘗試的IndexOf找到一個引號字符

PDFReference =

javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("InvestorL 
    ibrary_ILTocUC$LandingPanel$ildetail$lbNext", "", false, "", "/AO/Main.aspx?did 
    = [email protected]!nb2016!n160550603", false, true)) 

從以上我只是想捕捉[email protected]!nb2016!n160550603

所以這是我想:

$PDFDid = $PDFReference.Substring($PDFReference.IndexOf('?did = ') + 7, ($PDFReference.IndexOf('?did = ') + 7) - ($PDFReference.IndexOf("`""))) 

我的問題是與$PDFReference.IndexOf("「」)`,因爲這似乎是乾淨的方法來捕獲完整的字符串,但我得到一個錯誤

Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the 
string. 
Parameter name: length" 

任何想法如何執行IndexOf一個"人物?我試過使用""",但是這些編碼都沒有工作。它不會識別報價。

回答

2

你已經在做IndexOf的權利,雖然我會用.indexOf('"')。錯誤依賴於Substring。不過,我不會走這條路......


你可以使用一個簡單的regex容易捕捉到的字符串:

\?did\s+=\s+([^"]*) 

Regular expression visualization

PowerShell的

$PDFDid = [regex]::Match($PDFReference , '\?did\s+=\s+([^"]*)').Groups[1].Value 
+0

令人驚歎。正是我需要的。我現在試着剖析一下,以確保我完全理解它向前邁進。謝謝! – Quanda

+0

我編輯了我的答案,並添加了一些信息到您原來的問題。然而,正則表達式在這裏很適合,因爲你想捕獲一個字符串,否則你必須使用一些'indexof'和'substring',這很難閱讀恕我直言。 –

+1

你可以在這裏找到更多關於這個正則表達式的信息:https://regex101.com/r/iN9hR2/1 –

相關問題