试图找到IndexOf引号字符

时间:2016-06-08 13:12:02

标签: powershell character-encoding escaping substring indexof

全字符串:

PDFReference =

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

从上面我只想捕获003711812@bull!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 "个字符的任何想法?我尝试过使用""",但这些编码都不起作用。它不会认出这句话。

1 个答案:

答案 0 :(得分:2)

您已经在IndexOf权限,但我会使用.indexOf('"')。错误依赖于Substring。但是,我不会这样......

您可以使用简单的正则表达式轻松捕获字符串:

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

Regular expression visualization

<强>的PowerShell

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