我是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