powershell - 试图找到 IndexOf 引号字符

标签 powershell character-encoding escaping substring indexof

完整字符串:

PDF引用=

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 " 字符吗?我尝试过使用 ", " 、 和 " 但这些编码都不起作用。它不会识别报价。

最佳答案

尽管我会使用 .indexOf('"'),但您已经正确执行了 IndexOf。该错误依赖于 Substring。不过,我不会走这条路...


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

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

Regular expression visualization

PowerShell:

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

关于powershell - 试图找到 IndexOf 引号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703517/

相关文章:

powershell - "Parameter set cannot be resolved"当管道参数与其他参数的特定组合时

powershell - 在PowerShell中将变量传递到语句 block 括号中

javascript - 是否可以将包含 "high"unicode 字符的字符串转换为由从 utf-32 ("real") 代码派生的 dec 值组成的数组?

string - 将 utf-8 编码的字符串从字节转换为字符时,计算机如何知道字符在哪里结束?

parsing - 当不可避免的字符被转义时该怎么办?

powershell - 如何从 Azure 数据工厂运行 PowerShell

json 文件到 powershell 并返回到 json 文件

R 绘图 : Corrupted/missing characters for devices other than pdf

linux - bash 文件名中引号的处理

javascript - 是否有不同类型的转义序列?