正则表达式捕获每个字母到同一个捕获组

时间:2014-05-11 09:17:54

标签: regex powershell

我要解析的字符串由空格分隔的字母组成。字母只能出现零次或一次。一个例子是" A B C D E""C E D"。我需要一个模式,将所有字母的外观捕获到同一个捕获组中,以便稍后我可以遍历该组。

" A B C D E " -match "<regex>"

我希望结果是:

Group 'Letter' match #1: A
Group 'Letter' match #2: B
Group 'Letter' match #3: C
Group 'Letter' match #4: D
Group 'Letter' match #5: E

1 个答案:

答案 0 :(得分:2)

在Powershell中,以下内容为您提供了所有正则表达式匹配的数组:

$regex = [regex] '\b\p{L}\b'
$allmatches = $regex.Matches(" A B C D E FG");

然后,您可以使用$allmatches.Item[](包含"A", "B", "C", "D", "E"但不包含"F""G")来访问匹配项。