1

I have tab delimited data in an array and applying the regex match as shown below i am able to get the id value as required.

However the $matches variable returns two values and they are the same value.

$data = @(
            'name1  "1001"  role1',
            'name2  "1002"  role2'
        )

foreach($d in $data)
{
    $d -match '(?<=")(.*)(?=")' | out-null #search for id in quotes
    $id = $matches[0]
    #...  
}

When I check $matches I get 2 same matches instead of 1 -

PS C:\> $matches

Name                           Value                                           
----                           -----                                           
1                              1002                                            
0                              1002

I am a regex beginner. I got the regex string to apply from here (Regex Match all characters between two strings).

So I either need to tweak my regex logic or there is a reason why $matches returns two same values for a single match. I tried searching for a relevant answer but was not able to find an article to explain this. Can someone please advice on what is happening?

Community
  • 1
  • 1
Ron
  • 49
  • 1
  • 9

1 Answers1

2

In $matches[0] you find the complete match

In $matches[1] you find the content of the first capturing group.

==> You don't need a capturing group around the whole pattern, so just remove the brackets

'(?<=").*(?=")'
stema
  • 90,351
  • 20
  • 107
  • 135
  • Aha! So it was a regex tweak! Thanks for the reference link. While i am still trying to understand how a capturing group works, i am glad to see just 1 match as expected. – Ron May 08 '14 at 10:35