从 powershell 中的字符串搜索中提取整个单词

Extracting a whole word from a string search in powershell

我在一个文件夹中有多个文件,这些文件具有唯一链接。它们包含带有域名的单词,文本文件中没有任何特定顺序的链接。例如:

From:mail.usn.tn.com , Transporder via: exo2.reg.nt.com , fj.ter.usa

我一直在尝试使用 powershell 来完成以下操作:查找包含“.com”的单词然后提取整个单词,因此在这种情况下它会提取前两个而不是最后一个。

我将其用作我的代码

Get-ChildItem  -Filter *.txt | Select-String -Pattern '^.com' | Select -ExpandProperty line | Set-Content "Output.txt"

这应该可以解决问题:

Get-ChildItem  -Filter *.txt | Select-String -Pattern '([a-z0-9.]+\.com)' -AllMatches | % { $_.Matches } | % { $_.Value } 

你可以在这里测试:https://regex101.com/r/ojTzVX/1