locked
Powershell and $matches RRS feed

  • 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                               test   

     

    If 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    :  test

    Groups   : { test}
    Success  : True
    Captures : { test}
    Index    : 28
    Length   : 5
    Value    :  test

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    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    : test

     

    Thursday, 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                               test   

     

    If 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    :  test

    Groups   : { test}
    Success  : True
    Captures : { test}
    Index    : 28
    Length   : 5
    Value    :  test

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    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