【发布时间】:2021-11-14 21:26:50
【问题描述】:
我是 powershell 新手。我正在尝试使我的工作自动化一点,并且需要从所有文件类型中简单地提取以下模式:
([0-9A-Z]{2,4}.[0-9A-Z]{8}.[0-9A-Z]{8}.[0-9A-Z]{4})
例子:
*lots of text*
X-xdaemon-transaction-id: string=9971.0A67341C.6147B834.0043,ee=3,shh,rec=0.0,recu=0.0,reid=0.0,cu=3,cld=1
X-xdaemon-transaction-id: string=AA71.0A67341C.6147B442.0043,ee=3,shh,rec=0.0,recu=0.0,reip=0.0,cu=3,cld=1
*lots of text*
不幸的是,我收到这样的输出:
1mAAAA-0005nG-TN-H:220:
X-xdaemon-transaction-id: string=AA71.0A67341C.6147B442.0043,ee=3,shh,rec=0.0,recu=0.0,reip=0.0,cu=3,cld=1
我的“代码”如下:
Select-String -Path C:\Samples\* -Pattern "(0001.[0-9A-Z]{8}.[0-9A-Z]{8}.[0-9A-Z]{4})" -CaseSensitive
我只想接收模式:AA71.0A67341C.6147B442.0043,不添加任何内容
感谢您的帮助!
【问题讨论】:
-
试试
'\b[0-9A-Z]{2,4}\.[0-9A-Z]{8}\.[0-9A-Z]{8}\.[0-9A-Z]{4}\b' -
嗨!感谢您的快速回复。不幸的是,同样的结果 :( Powershell 添加了关于他找到它的文件的附加信息,行和整行到匹配的模式。
PS C:\Xray\> Select-String -Path C:\Xray\* -Pattern '\b[0-9A-Z]{4}\.[0-9A-Z]{8}\.[0-9A-Z]{8}\.[0-9A-Z]{4}\b' -CaseSensitive输出:1mAAAA-0005nG-TN-H:220: X-xdaemon-transaction-id: string=AA71.0A67341C.6147B442.0043,ee=3,shh,rec=0.0,recu=0.0,reip=0.0,cu=3,cld=1 -
Select-String返回MatchInfo对象。我建议您注意Select-String和 theMatchInfoclass 上的 Microsoft Docs。您可能需要考虑使用-matchoperator 是否更适合您的需求。 -
@WiktorStribiżew 感谢您的正则表达式,它比我的要好得多。我找到了如何做我问的事情:我必须创建一个环境变量 $B = Select-String -Path C:\Samples* -Pattern '\b[0-9A-Z]{2,4}\. [0-9A-Z]{8}\.[0-9A-Z]{8}\.[0-9A-Z]{4}\b' -CaseSensitive -Value $B.Matches.Value 它给出我寻找的结果
标签: regex powershell