locked
Strange behavior for -notmatch RRS feed

  • Question

  • Hi Guys

    I'm driving crazy : I'm writing a simple line of code but the result is not at all what I expect and I really don't understand why. It should be something pretty stupid but I can't find it

    here 's the code :

    $members= @('DOMAIN/FR_SQL','FRPCL','FRQL','DOMAIN/FRWBTEST', 'DOMAIN/LGFRADMIN')  |Where-Object {$_ -notmatch ('LG*|GG*')}
    

    The result is : 

    FRPCL , DOMAIN/FRWBTEST

    Where I expect  : 

    DOMAIN/FRWBTEST, FRPCL, FRQL, DOMAIN/FR_SQL

    Can someone explain me why 'DOMAIN/FR_SQL' & 'FRQL' match the search criteria ?

    thx

    Wednesday, April 18, 2018 11:59 AM

Answers

  • You need to write your -notmatch like this:

     Where-Object { $_ -notmatch 'LG|GG' }

    • Marked as answer by orlith Wednesday, April 18, 2018 12:15 PM
    Wednesday, April 18, 2018 12:11 PM

All replies

  • You need to write your -notmatch like this:

     Where-Object { $_ -notmatch 'LG|GG' }

    • Marked as answer by orlith Wednesday, April 18, 2018 12:15 PM
    Wednesday, April 18, 2018 12:11 PM
  • Thanks TobyU

    So the wildcard mess up the result, I still don't understand why, but it solves the "issue" .

    Thanks for your help !

    Wednesday, April 18, 2018 12:15 PM
  • Since -match / -notmatch and therelike work with RegEx "*" is not seen as a wildcard it's seen as a quantifier.
    Wednesday, April 18, 2018 12:27 PM
  • Ok thanks !
    Wednesday, April 18, 2018 12:28 PM
  • -notmatch uses regex, so you can use '.*' for a wildcard, although you don't need it.


    echo DOMAIN/FR_SQL FRPCL FRQL DOMAIN/FRWBTEST DOMAIN/LGFRADMIN |
      where { $_ -notmatch 'lg.*|gg.*' }  


    • Edited by JS2010 Wednesday, April 18, 2018 4:36 PM
    Wednesday, April 18, 2018 2:07 PM