Powershell匹配重复仅显示最后一次出现

时间:2013-02-14 15:48:30

标签: regex powershell match

使用Powershell使用-match将字符串分解为模式我只看到模式的最后一次出现:

$a = "1 0 8 string a 8 string b 10 final string"
$a -match '^(\d+) (\d+) ((\d+) (\D+)){0,}'
$matches

显示

True

Name   Value
----   -----
5      final string
4      10
3      10 final string
2      0
1      1
0      1 0 8 string a 8 string b 10 final string

如何填写“8”,“string a”,“8”,“string b”等?

谢谢!

2 个答案:

答案 0 :(得分:1)

你想要这个吗?

([regex]::Matches($a,'^(\d+) (\d+) ((\d+) (\D+)){0,}') | select -expa groups )[3] |
 select -expa captures | select -expa value | -split '(\d+)' | ? { $_ } | % { $_.trim() }

((([regex]::Matches($a,'^(\d+) (\d+) ((\d+) (\D+)){0,}') | select -expa groups )[3] | select -expa captures | select -expa value) -split '(\d+)' | ? { $_ } | % { $_.trim() } | 
% { "`"$_`""} ) -join ','

答案 1 :(得分:0)

我相信您可以使用类似$matches.Group[3].Capture[0]等的内容来获取该单个小组匹配的所有文字。

我对powershell语法不太熟悉,但这里是对你需要的库的引用:
http://msdn.microsoft.com/en-us/library/30wbz966.aspx#CaptureCollection

相关问题