我有一個文本檔案,內容可以是:
debug --configuration "Release" \p corebuild
或者:
-c "Dev" debug
現在我必須驗證檔案以查看它是否有任何匹配的模式,--configuration或者-c在它旁邊列印字串
- 模式 1 - 應該是
Release - 模式 2 - 應該是
Dev
如何在單個命令中實作這一點?
我在下面嘗試過,但不確定如何僅提取文本中的版本,我一次只嘗試查看 1 個模式
PS Z:\> $text = Get-Content 'your_file_path' -raw
PS Z:\> $Regex = [Regex]::new("(?<=\-\-configuration)(.*)")
PS Z:\> $Match = $Regex.Match($text)
PS Z:\> $Match.Value
**Release /p net**
任何幫助,將不勝感激
uj5u.com熱心網友回復:
如果我理解正確并且您只關心提取引數的引數而不是使用哪個引數,那么這可能會奏效:
$content = Get-Content 'your_file_path' -Raw
$re = [regex] '(?i)(?<=(?:--configuration|-c)\s")[^"] '
$re.Matches($content).Value
有關詳細資訊,請參閱https://regex101.com/r/d2th35/3。
從評論中的反饋--configuration可以-c一起出現,因此Regex.Matches需要找到所有出現的情況。
uj5u.com熱心網友回復:
Get-Content 'your_file_path' |
ForEach-Object { if ($_ -match ' (?:--configuration|-c) "(.*?)"') { $Matches.1 } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527282.html
標籤:正则表达式电源外壳
上一篇:函式內的malloc不改變指標值
