Regex Powershell显示太多



我是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,不添加任何东西

谢谢你的帮助!

可以使用

$rx = 'b[0-9A-Z]{2,4}.[0-9A-Z]{8}.[0-9A-Z]{8}.[0-9A-Z]{4}b'
Select-String -AllMatches -Pattern $rx -Path 'C:Samples*' -CaseSensitive | % { $_.matches.value }

,

  1. 添加单词边界以匹配您期望的字符串作为整个单词,并转义文字.字符
  2. 使用-AllMatches(每行获得多个匹配)并使用$_.matches.value访问每个结果对象匹配值。
  3. PS测试:

PS C:Usersadmin> $B = Select-String -AllMatches -Pattern 'b[0-9A-Z]{2,4}.[0-9A-Z]{8}.[0-9A-Z]{8}.[0-9A-Z]{4}b' -Path 'C:Samples*' -CaseSensitive | % { $_.matches.value }
PS C:Usersadmin> $B
9971.0A67341C.6147B834.0043
AA71.0A67341C.6147B442.0043
PS C:Usersadmin>

try:

找到美元= Get-ChildItem * . txt |选择string b模式" [0-9A-Z]{2,4}。[0-9A-Z]{8}。[0-9A-Z]{8}。[0-9A-Z] {4} b -CaseSensitive

find.Matches.Value美元

最新更新