Answered by:
Powershell and $matches

Question
-
I'm curious why $matches is only returning one item in the below example when there are two? Thanks in advance.
$text = "This is a test. How was your test?"
$text -match '.test'
$matches
Thursday, August 4, 2011 1:59 AM
Answers
-
-match returns as soon as it finds a match in the string.
$matches[0] will always be the matched component. The subsequent array elements will be any capture groups within the regex.
$text -match ".(te)(st)"
True
Name Value
---- -----
2 st
1 te
0 testIf you're after multiple matches in the same string, you can use the static matches method of regex:
[regex]::matches($text,".test")
Groups : { test}
Success : True
Captures : { test}
Index : 9
Length : 5
Value : testGroups : { test}
Success : True
Captures : { test}
Index : 28
Length : 5
Value : test
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "- Proposed as answer by Lincoln Atkinson Thursday, August 4, 2011 2:33 PM
- Marked as answer by Tiger LiMicrosoft employee Wednesday, August 10, 2011 1:36 AM
Thursday, August 4, 2011 2:22 AM
All replies
-
I'm still looking for a reason why that doesn't work, but here are a couple of other ways to accomplish finding all of the matches:
("This is a test. How was your test?" | Select-String -pattern "test" -AllMatches).matches
Groups : {test}
Success : True
Captures : {test}
Index : 10
Length : 4
Value : test
Groups : {test}
Success : True
Captures : {test}
Index : 29
Length : 4
Value : test[regex]::Matches("This is a test. How was your test?","test")
Groups : {test}
Success : True
Captures : {test}
Index : 10
Length : 4
Value : test
Groups : {test}
Success : True
Captures : {test}
Index : 29
Length : 4
Value : testThursday, August 4, 2011 2:14 AM -
Ok, looks like PowerShell doesn't support a global match using the -match operator. So the examples I posted above are the best ways to perform a global match. See link below for more information at about halfway through the document.
http://www.johndcook.com/regex.html
Connect link regarding this as well.
https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=264229&SiteID=99
Thursday, August 4, 2011 2:18 AM -
-match returns as soon as it finds a match in the string.
$matches[0] will always be the matched component. The subsequent array elements will be any capture groups within the regex.
$text -match ".(te)(st)"
True
Name Value
---- -----
2 st
1 te
0 testIf you're after multiple matches in the same string, you can use the static matches method of regex:
[regex]::matches($text,".test")
Groups : { test}
Success : True
Captures : { test}
Index : 9
Length : 5
Value : testGroups : { test}
Success : True
Captures : { test}
Index : 28
Length : 5
Value : test
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "- Proposed as answer by Lincoln Atkinson Thursday, August 4, 2011 2:33 PM
- Marked as answer by Tiger LiMicrosoft employee Wednesday, August 10, 2011 1:36 AM
Thursday, August 4, 2011 2:22 AM -
The way the capture group enumeration works in $matches, if it did do a global match return, it would result in unpredicatable enumeration of the captures.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "Thursday, August 4, 2011 1:36 PM