powershell 从文本文件中提取url

nuypyhwy  于 2023-06-06  发布在  Shell
关注(0)|答案(2)|浏览(431)

我有一个大的文本文件,其中包含的文本查看此电子邮件在您的浏览器,然后一个网址。它可以变化,有时部分URL会转到下一行。
此外,当它确实进入下一行时,在末尾有一个需要删除的等号,而不是任何其他可能存在的等号。
几个例子:

  1. View this email in your browser (https://us15.campaign-archive.com/?e=3D1460&u=3Df6e2bb1612577510b&id=3D2c8be)
  2. View this email in your browser <https://mail.com/?e=3D14=
  3. 60&u=3Df612577510b&id=3D2c8be>
  4. View this email in your browser (https://eg.com/?e=3D1460&u=3Df6510b&id=3D2c8be)

我需要使用PowerShell提取该URL,不带方括号(括号),有时可以是< >,以便我可以将其作为HTML文件下载。

  1. if ($str -match '(?<=\()https?://[^)]+') {
  2. # # ... remove any line breaks from it, and output the result.
  3. $Matches.0 -replace '\r?\n'
  4. }
  5. if ($str -match '(?<=\<)https?://[^>]+') {
  6. # # ... remove any line breaks from it, and output the result.
  7. $Matches.0 -replace '\r?\n'
  8. }
uxh89sit

uxh89sit1#

  • 因为你试图匹配 * 跨行 *,你需要确保你的文本文件是 * 作为一个整体 * 阅读的,即。作为单个多行字符串,您可以使用Get-Contentcmdlet的-Raw开关来执行此操作。
  • 除此之外,正则表达式中唯一缺少的是匹配并删除换行符之前的=

以下代码从输入文件file.txt中提取所有URL,并将它们输出为字符串数组(删除换行符和行尾=):

  1. # Note the '=' before '\r?\n'
  2. [regex]::Matches(
  3. (Get-Content -Raw file.txt),
  4. '(?<=[<(])https://[^>)]+'
  5. ).Value -replace '=\r?\n'
  • 直接使用[regex]::Matches() .NET API允许您一次提取所有匹配项,而PowerShell的-match运算符只查找一个匹配项。
  • 有关将来引入-matchall运算符的建议,请参阅GitHub issue #7867
  • 然后使用-replace从匹配项中删除换行符(\r?\n)沿着前面的=

有关URL匹配正则表达式的解释和使用它的能力,请参阅this regex101.com page
使用多行字符串文字的示例:

  1. [regex]::Matches('
  2. View this email in your browser (https://us15.campaign-archive.com/?e=3D1460&u=3Df6e2bb1612577510b&id=3D2c8be)
  3. View this email in your browser <https://mail.com/?e=3D14=
  4. 60&u=3Df612577510b&id=3D2c8be>
  5. View this email in your browser (https://eg.com/?e=3D1460&u=3Df6510b&id=3D2c8be)
  6. ',
  7. '(?<=[<(])https://[^>)]+'
  8. ).Value -replace '=\r?\n'

输出:

  1. https://us15.campaign-archive.com/?e=3D1460&u=3Df6e2bb1612577510b&id=3D2c8be
  2. https://mail.com/?e=3D1460&u=3Df612577510b&id=3D2c8be
  3. https://eg.com/?e=3D1460&u=3Df6510b&id=3D2c8be
展开查看全部
huus2vyu

huus2vyu2#

此解决方案适用于您提供的示例:

  1. $text = @(
  2. 'View this email in your browser (https://us15.campaign-archive.com/?e=3D1460&u=3Df6e2bb1612577510b&id=3D2c8be)',
  3. 'View this email in your browser <https://mail.com/?e=3D14=
  4. 60&u=3Df612577510b&id=3D2c8be>',
  5. 'View this email in your browser (https://eg.com/?e=3D1460&u=3Df6510b&id=3D2c8be)'
  6. )
  7. $text = $text | ForEach-Object {
  8. $PSItem.Replace('<','(').Replace('>',')').Replace("=`n",'').Split('(')[1].Replace(')','')
  9. }

输出如下所示:

  1. https://us15.campaign-archive.com/?e=3D1460&u=3Df6e2bb1612577510b&id=3D2c8be
  2. https://mail.com/?e=3D1460&u=3Df612577510b&id=3D2c8be
  3. https://eg.com/?e=3D1460&u=3Df6510b&id=3D2c8be

我只使用replace而不使用regex。您在拆分url时遇到的困难可以通过执行

  1. .Replace("=`n")
展开查看全部

相关问题