Question

My string to be parsed consist of letters separated by spaces. Letters can only occur zero or one time. An example is " A B C D E" or "C E D". I need a pattern that captures appearances of all letters into the same capture group so that I can iterate over that group later.

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

I expect the result to be:

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
Was it helpful?

Solution

In Powershell, the following gives you an array of all regex matches:

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

You can then access the matches using $allmatches.Item[] (which will contain "A", "B", "C", "D", "E" but not "F" or "G").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top