簡體   English   中英

如何修改此正則表達式以在Powershell中工作

[英]How to modify this regex to work in Powershell

所以我有這個正則表達式https://regex101.com/r/xG8oX2/2 ,它給了我想要的匹配。

但是當我運行這個powershell腳本時,它沒有給我任何匹配。 我應該在這個正則表達式中修改什么才能在powershell中獲得相同的匹配?

$pattern2 = '\d{4}\/\d{2}\/\d{2}.*]\s(?<reportHash>.*):.*Start.*\r*\n*.*\n.*ReportLayoutID=(\d{1,7})';
$reportLayoutIDList = Get-Content -Path bigOptions.txt | Out-String |
Select-String -Pattern $pattern2 -AllMatches |
Select-Object -ExpandProperty Matches |
Select-Object @{n="ReportHash";e={$_.Groups["reportHash"]}},
              @{n="LayoutID";e={$_.Groups["reportLayoutID"]}};$reportLayoutIDList | 
Export-csv reportLayoutIDList.csv;

問題是你的換行。 在Windows中,換行符是CRLF( \\r\\n ),而在UNIX等中,它們只是LF \\n

因此,無論您需要修改的輸入,只使用LF或您需要更換\\n\\r\\n在你的正則表達式。

正如@briantist所提到的,使用\\r?\\n將匹配任一方式。

感謝Frode F和briantist。

這是在Powershell中使用的正則表達式模式:

$pattern2 = '\\d{4}\\/\\d{2}\\/\\d{2}.*]\\s(?<reportHash>.*):.*Start.*[\\r?\\n]*.*[\\r?\\n].*ReportLayoutID=(?<reportLayoutID>\\d+)';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM