我是Powershell的新手,嘗試使用RegEx解析多個字符串(非分隔符)。默認的RegEx輸出使用$匹配,所以試圖保存來自第一個字符串,第二個字符串,第三個字符串等的值。所以我可以稍後使用「解析」值。使用PowerShell輸出多個RegEx匹配到另一個陣列
我想不出如何運行並將多行輸出保存到新數組中,以便日後檢索值?
- 設置正則表達式匹配字符串
- 設置1對多$字符串變量
- 集1個$總可變結合許多$字符串變量形成步驟2
- 的foreach變量IN $總,運行正則表達式解析字符串到單獨的值
#Work
$regex = "([A-Z]*\..*\.[A-Z]?) (?:\s*) ([A-Za-z]{3}\s\d{1,2}, \d{4} \d{2}:\d{2}:\d{2}) ([A-Za-z]{3}\s\d{1,2}, \d{4} \d{2}:\d{2}:\d{2}) ([A-Z]{2}) (\d*)/(\d) (\d)"
$string01 = "DEV.This_Is_Command_JobA.C Jun 7, 2016 07:33:35 Jun 7, 2016 07:59:22 SU 84534137/1 0"
$string02 = "DEV.This_Is_Command_JobB.C Jun 8, 2016 08:33:35 Jun 8, 2016 08:59:22 SU 84534138/1 0"
$string03 = "DEV.This_Is_Command_JobC.C Jun 9, 2016 09:33:35 Jun 9, 2016 09:59:22 SU 84534139/1 0"
$total = $string01,$string02,$string03
Foreach ($_ in $total)
{
$_ -match $regex
}
#check work
$matches
所需的輸出:
7 0 6 1 5 84534139 4 SU 3 Jun 7, 2016 07:59:22 2 Jun 7, 2016 07:33:35 1 DEV.This_Is_Command_JobA.C 0 DEV.This_Is_Command_JobA.C 7 0 6 1 5 84534139 4 SU 3 Jun 8, 2016 08:59:22 2 Jun 8, 2016 08:33:35 1 DEV.This_Is_Command_JobB.C 0 DEV.This_Is_Command_JobB.C 7 0 6 1 5 84534139 4 SU 3 Jun 9, 2016 09:59:22 2 Jun 9, 2016 09:33:35 1 DEV.This_Is_Command_JobC.C 0 DEV.This_Is_Command_JobC.C So I can retrieve values such as an example: $matchesA[0-7] $matchesB[0-7] $matchesC[0-7]
那麼最新的問題? – 4c74356b41
'$ Result = @($ total |%{[Regex] :: Match($ _,$ regex)})' – PetSerAl
請清楚地突出顯示主要問題,不是很清楚嗎? – SACn