Unexpected regular expression groups capture

Bernard BOROWSKI 1 Reputation point


Trying to get subcaptures with the following string and regular expression, I'm surprised by the result I get. Can someone explain me the result. Is the NFA engine doing bad job ?

I'm working under Powershell. So, I imagine with, using the Windows .NET framework.

My string :

"p1  p2  p3,'   ''  ',   p4"

I put it between quotes at it includes several couples of apostrophes. One with 3 spaces between, the second, immediatly following with 2 spaces inside.

This string wholy match the following regular expression :

^(\w*)\s+(\w+)(([^' ]*)|('[^']*')|\s?)*$

Indeed :

"p1  p2  p3,'   ''  ',   p4" -match "^(\w*)\s+(\w+)(([^' ]*)|('[^']*')|\s?)*$"

results to :


But :



Name                       Value
----                           -----
5                               '  '
2                              p2
1                              p1
0                              p1  p2  p3,'   ''  ',   p4

I would have expected much more. That is (not listing 1st level subgroup with the alternative 2nd level subgroups)

' '
' '

Why "p3,", "' '","' '" and "," are not captured ?

"p1  p2  p3,'   ''  ',   p4" -match "^(\w*)\s+(\w+)(?:([^' ]*)|('[^']*')|\s?)*$"

so with a non capturing 1st level group, do not gives a better result.
It simply gives one less (empty) submatch. That is, expectedly, minus the 1st level subgroup I guess.

Name Value

4 ' '
2 p2
1 p1
0 p1 p2 p3,' '' ', p4

Do I miss the equivalent of the "global" Ecmascript flag of regexp objects ?
However I get the same result using javacript.

So what's wrong ?

Thank for the help.

.NET Standard
.NET Standard
A formal specification of .NET APIs that are available on multiple .NET implementations.
505 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: The period of time during which a program is being executed in a computer.
998 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 35,796 Reputation points

    Use this website. Enter your values. It gives a good explanation of your matches


    0 comments No comments

  2. Bernard BOROWSKI 1 Reputation point

    Thanks for the website indication. But not as useful as the information I missed which is the grouping-constructs-in-regular-expressions explanation in the .NET reference site.

    It gives the rule when a quantifier is applied to a capturing group. And the way to get the "previous" captures for a group while corresponding match item gives only the last.

    0 comments No comments