header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Help with regex expression?
Last Post 22 Apr 2009 05:15 AM by get-james. 12 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
get-jamesUser is Offline
New Member
New Member
Posts:59
Avatar

--
22 Apr 2009 01:52 AM
    Hi Guys,

    Need a little help with regex expressions,  I am trying to only select one item from a collection, using the example below, I would only like to match "User 01 (ABC Team)":

    $a = 'User 01 (ABC Team)','User 01 (ABCDE Team)','ABCUser 01 (XYZ Team)','ABCDUser 01 (X Team)','UserABCD 01 (J Team)'

    $a | where {$_ -match "ABC"}
     
    User 01 (ABC Team)
    User 01 (ABCDE Team)
    ABCUser 01 (XYZ Team)
    ABCDUser 01 (X Team)
    UserABCD 01 (J Team)

    What do I need to specify in the match parameter, to do this?

    Cheers
    James
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    22 Apr 2009 02:15 AM
    # with -match operator
    PS> $a -match '\(ABC Team\)'
    User 01 (ABC Team)

    # using where-object and -match operator
    PS > $a | where {$_ -match '(abc team)'}
    User 01 (ABC Team)

    # with -eq operator
    PS > $a -eq 'User 01 (ABC Team)'
    User 01 (ABC Team)


    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Apr 2009 02:22 AM
    Cheers Shay,

    What if I only want to match "ABC" on its own?
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Apr 2009 02:29 AM
    Shay,

    I think I might have done it with the following:


    PS C:\> $a | where {$_ -match "\s.(ABC)\s"}

    User 01 (ABC Team)



    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    22 Apr 2009 02:35 AM
    I'm not sure I get the question myself, but I'll take a swing at it...

    #Match any sting with ABC
    PS > $a -match "ABC\s"

    ~Glenn
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Apr 2009 02:39 AM
    Glenn,

    Just to give you some more information,

    I have a large CSV file, with loads of user displaynames in it.

    For example:

    Brown, James (ABC Team)
    Levy, Shay (Powershell dude - Team ABC)
    Unknown, Glenn (Team PS)
    ABCDen, Pete (Unix Team)
    Black, Jane (Z&ABC Team)
    Smith, Matt (North ABC)
    Yellow, Nigel (South ABC)


    What I want is to pick out only the following users from the above list:

    Brown, James (ABC Team)
    Levy, Shay (Powershell dude - Team ABC)
    Smith, Matt (North ABC)
    Yellow, Nigel (South ABC)

    Hope this helps clear up what I am tryng to achive?
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Apr 2009 02:53 AM

    $a = "Brown, James (ABC Team)","Levy, Shay (Powershell dude - Team ABC)","Unknown, Glenn (Team PS)","ABCDen, Pete (Unix Team)","Black, Jane (Z&ABC Team)","Smith, Matt (North ABC)","Yellow, Nigel (South ABC)"

    $a | where {$_ -match "\s.(abc)\s"}
    Brown, James (ABC Team)

    That doesn't work?

    Will keep trying...
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    22 Apr 2009 02:56 AM
    Okay, I get it now... I would use something like this,

     PS > $a -match "(\(ABC\s)|(\sABC\))"
     
    ~Glenn
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    22 Apr 2009 02:56 AM
    Try this, I named the columns in the csv file as last,first:

    # match '(abc' OR 'abc)' users
    PS > ipcsv users.csv | where {$_.first -match '\(abc|abc\)'}

    Last First
    ---- -----
    Brown James (ABC Team)
    Levy Shay (Powershell dude - Team ABC)
    Smith Matt (North ABC)
    Yellow Nigel (South ABC)


    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Apr 2009 03:02 AM
    Cheers Guys,

    Nearly there...

    It is now not matching some display names like "ABC Memebers":


    $a = "Brown, James (ABC Team)","Levy, Shay (Powershell dude - Team ABC)","Unknown, Glenn (Team PS)","ABCDen, Pete (Unix Team)","Black, Jane (Z&ABC Team)","Smith, Matt (North ABC)","Yellow, Nigel (South ABC)","Team ABC Meeting","ABC Members"

    PS C:\> $a
    Brown, James (ABC Team)
    Levy, Shay (Powershell dude - Team ABC)
    Unknown, Glenn (Team PS)
    ABCDen, Pete (Unix Team)
    Black, Jane (Z&ABC Team)
    Smith, Matt (North ABC)
    Yellow, Nigel (South ABC)
    Team ABC Meeting
    ABC Members


    Cheers
    James

    PS C:\> $a | where {$_ -match "(\(ABC)\s|(\sABC\))"}
    Brown, James (ABC Team)
    Levy, Shay (Powershell dude - Team ABC)
    Smith, Matt (North ABC)
    Yellow, Nigel (South ABC)

    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    22 Apr 2009 03:14 AM
    The regex patter may very depending on your needs, here's one way:

    PS > $a -match '\(abc|abc\)|abc members'

    Brown, James (ABC Team)
    Levy, Shay (Powershell dude - Team ABC)
    Smith, Matt (North ABC)
    Yellow, Nigel (South ABC)
    ABC Members

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    22 Apr 2009 03:15 AM
    okay...
    Match any string that starts with abc, or contains abc, or contains (ABC, or contains ABC)

    PS > $a -match "^ABC\s|\sABC\s|(\(ABC\s)|(\sABC\))"

    ~Glenn
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Apr 2009 05:15 AM
    Thanks both very much for your help...

    The following parameter from Glenn, does the job:

    $a -match "^ABC\s|\sABC\s|(\(ABC\s)|(\sABC\))"

    I am learning new things every day :-)

    Cheers again.
    James



    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer