Windows Vista Forums

Using RegEx to get an exact match

  1. #1


    Steven Guest

    Using RegEx to get an exact match

    I'm working on a PowerShell script that will clean up a file that has a
    list of names to be moved to Exchange 2007 based off an exclusion list.
    This is because I have some mailboxes that I don't want moved (for one
    reason or another). So what I want to do is take fileA and search
    through it for any user names in fileB and pull them out and then save
    the results as a "clean" file.

    The problem I've run into is that when using the -replace regex , it's
    not performing exact matches (at least as I had expected it to). For
    example:

    fileA:
    userA
    userAA
    userB
    userBB

    fileB:
    userA
    userB

    My script looks like this:

    #####################
    # Cleaning up the list so all entries are sorted, unique, and in lower
    case
    $moveList = get-content fileA.txt | sort-object -uniq | foreach-object {
    $_.ToLower() }
    # Cleaning up the list so all entries are sorted, unique, in lower case,
    and if they put in the domain portion of the account
    # I strip that out.
    $exclude = get-content fileB.txt | sort-object -uniq | foreach-object {
    $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}

    # Go through each object in $exclude and remove any matching object in
    $moveList
    $exclude | foreach-object { $moveList = $moveList -replace $_ }



    # Clean up whitespace left by replace operation
    $moveList = $moveList -match "^\w+"

    # Output clean file to file to be used as an answer file for mailbox
    moves.
    $moveList | out-file -encoding ASCII CleanUserList.txt
    #####################

    The problem that I'm seeing is that "$moveList -replace $_" is not
    hitting exact matches. So in the above example if userA was in $_, it
    is matching:

    userA
    userAA

    What I'm left with is "A" because it is stripping out the "userA" part.
    How can I have it match only "userA" and not userAA" or better yet, does
    anyone have a better way doing what I'm doing?

    Thanks for any suggestions..as always I really appreciate it.



      My System SpecsSystem Spec

  2. #2


    Shay Levi Guest

    Re: Using RegEx to get an exact match


    Not sure I'm following you. Based on FileA and FileB, What would be the content
    of CleanUserList.txt?



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I'm working on a PowerShell script that will clean up a file that has
    > a list of names to be moved to Exchange 2007 based off an exclusion
    > list. This is because I have some mailboxes that I don't want moved
    > (for one reason or another). So what I want to do is take fileA and
    > search through it for any user names in fileB and pull them out and
    > then save the results as a "clean" file.
    >
    > The problem I've run into is that when using the -replace regex , it's
    > not performing exact matches (at least as I had expected it to). For
    > example:
    >
    > fileA:
    > userA
    > userAA
    > userB
    > userBB
    > fileB:
    > userA
    > userB
    > My script looks like this:
    >
    > #####################
    > # Cleaning up the list so all entries are sorted, unique, and in lower
    > case
    > $moveList = get-content fileA.txt | sort-object -uniq | foreach-object
    > {
    > $_.ToLower() }
    > # Cleaning up the list so all entries are sorted, unique, in lower
    > case,
    > and if they put in the domain portion of the account
    > # I strip that out.
    > $exclude = get-content fileB.txt | sort-object -uniq | foreach-object
    > {
    > $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    > # Go through each object in $exclude and remove any matching object in
    > $moveList
    > $exclude | foreach-object { $moveList = $moveList -replace $_ }
    > # Clean up whitespace left by replace operation
    > $moveList = $moveList -match "^\w+"
    > # Output clean file to file to be used as an answer file for mailbox
    > moves.
    > $moveList | out-file -encoding ASCII CleanUserList.txt
    > #####################
    > The problem that I'm seeing is that "$moveList -replace $_" is not
    > hitting exact matches. So in the above example if userA was in $_, it
    > is matching:
    >
    > userA
    > userAA
    > What I'm left with is "A" because it is stripping out the "userA"
    > part. How can I have it match only "userA" and not userAA" or better
    > yet, does anyone have a better way doing what I'm doing?
    >
    > Thanks for any suggestions..as always I really appreciate it.
    >


      My System SpecsSystem Spec

  3. #3


    Steven Guest

    Re: Using RegEx to get an exact match

    a
    b

    So the problem that I'm having is that in $_ is "userA" and it's
    matching both "userA" and "userAA". I only want it to match "userA".

    -----Original Message-----
    From: Shay Levi [mailto:no@xxxxxx]
    Posted At: Monday, January 28, 2008 5:37 PM
    Posted To: microsoft.public.windows.powershell
    Conversation: Using RegEx to get an exact match
    Subject: Re: Using RegEx to get an exact match


    Not sure I'm following you. Based on FileA and FileB, What would be the
    content
    of CleanUserList.txt?



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I'm working on a PowerShell script that will clean up a file that has
    > a list of names to be moved to Exchange 2007 based off an exclusion
    > list. This is because I have some mailboxes that I don't want moved
    > (for one reason or another). So what I want to do is take fileA and
    > search through it for any user names in fileB and pull them out and
    > then save the results as a "clean" file.
    >
    > The problem I've run into is that when using the -replace regex , it's
    > not performing exact matches (at least as I had expected it to). For
    > example:
    >
    > fileA:
    > userA
    > userAA
    > userB
    > userBB
    > fileB:
    > userA
    > userB
    > My script looks like this:
    >
    > #####################
    > # Cleaning up the list so all entries are sorted, unique, and in lower
    > case
    > $moveList = get-content fileA.txt | sort-object -uniq | foreach-object
    > {
    > $_.ToLower() }
    > # Cleaning up the list so all entries are sorted, unique, in lower
    > case,
    > and if they put in the domain portion of the account
    > # I strip that out.
    > $exclude = get-content fileB.txt | sort-object -uniq | foreach-object
    > {
    > $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    > # Go through each object in $exclude and remove any matching object in
    > $moveList
    > $exclude | foreach-object { $moveList = $moveList -replace $_ }
    > # Clean up whitespace left by replace operation
    > $moveList = $moveList -match "^\w+"
    > # Output clean file to file to be used as an answer file for mailbox
    > moves.
    > $moveList | out-file -encoding ASCII CleanUserList.txt
    > #####################
    > The problem that I'm seeing is that "$moveList -replace $_" is not
    > hitting exact matches. So in the above example if userA was in $_, it
    > is matching:
    >
    > userA
    > userAA
    > What I'm left with is "A" because it is stripping out the "userA"
    > part. How can I have it match only "userA" and not userAA" or better
    > yet, does anyone have a better way doing what I'm doing?
    >
    > Thanks for any suggestions..as always I really appreciate it.
    >


      My System SpecsSystem Spec

  4. #4


    Shay Levi Guest

    Re: Using RegEx to get an exact match

    I'm sure there's a shorter way , until then:

    $moveList = get-content fileA.txt | sort-object -uniq | foreach-object {
    $_.trim() }
    $exclude = get-content fileB.txt | sort-object -uniq | foreach-object {
    $_.trim() ; $moveList = $moveList -replace $_ }
    $moveList = $moveList -match "^\w+"
    $moveList | out-file -encoding ASCII .\CleanUserList.txt



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > a
    > b
    > So the problem that I'm having is that in $_ is "userA" and it's
    > matching both "userA" and "userAA". I only want it to match "userA".
    >
    > -----Original Message-----

    >> From: Shay Levi [mailto:no@xxxxxx]
    >> Posted At: Monday, January 28, 2008 5:37 PM
    >> Posted To: microsoft.public.windows.powershell
    >> Conversation: Using RegEx to get an exact match
    >> Subject: Re: Using RegEx to get an exact match
    >> Not sure I'm following you. Based on FileA and FileB, What would be
    >> the
    >> content
    >> of CleanUserList.txt?
    >> -----
    >> Shay Levi
    >> $cript Fanatic
    >> http://scriptolog.blogspot.com
    >> I'm working on a PowerShell script that will clean up a file that has
    >> a list of names to be moved to Exchange 2007 based off an exclusion
    >> list. This is because I have some mailboxes that I don't want moved
    >> (for one reason or another). So what I want to do is take fileA and
    >> search through it for any user names in fileB and pull them out and
    >> then save the results as a "clean" file.
    >>
    >> The problem I've run into is that when using the -replace regex ,
    >> it's not performing exact matches (at least as I had expected it to).
    >> For example:
    >>
    >> fileA:
    >> userA
    >> userAA
    >> userB
    >> userBB
    >> fileB:
    >> userA
    >> userB
    >> My script looks like this:
    >> #####################
    >> # Cleaning up the list so all entries are sorted, unique, and in
    >> lower
    >> case
    >> $moveList = get-content fileA.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.ToLower() }
    >> # Cleaning up the list so all entries are sorted, unique, in lower
    >> case,
    >> and if they put in the domain portion of the account
    >> # I strip that out.
    >> $exclude = get-content fileB.txt | sort-object -uniq | foreach-object
    >> {
    >> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    >> # Go through each object in $exclude and remove any matching object
    >> in
    >> $moveList
    >> $exclude | foreach-object { $moveList = $moveList -replace $_ }
    >> # Clean up whitespace left by replace operation
    >> $moveList = $moveList -match "^\w+"
    >> # Output clean file to file to be used as an answer file for mailbox
    >> moves.
    >> $moveList | out-file -encoding ASCII CleanUserList.txt
    >> #####################
    >> The problem that I'm seeing is that "$moveList -replace $_" is not
    >> hitting exact matches. So in the above example if userA was in $_,
    >> it
    >> is matching:
    >> userA
    >> userAA
    >> What I'm left with is "A" because it is stripping out the "userA"
    >> part. How can I have it match only "userA" and not userAA" or better
    >> yet, does anyone have a better way doing what I'm doing?
    >> Thanks for any suggestions..as always I really appreciate it.
    >>


      My System SpecsSystem Spec

  5. #5


    Shay Levi Guest

    Re: Using RegEx to get an exact match

    Modified version:

    $moveList = get-content fileA.txt | sort -unique
    get-content fileB.txt | sort -unique | foreach { $moveList = $moveList -replace
    $_ }
    $moveList | where {$_ -ne ""} | out-file -encoding ASCII .\CleanUserList.txt

    A
    B

    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I'm sure there's a shorter way , until then:
    >
    > $moveList = get-content fileA.txt | sort-object -uniq | foreach-object
    > {
    > $_.trim() }
    > $exclude = get-content fileB.txt | sort-object -uniq | foreach-object
    > {
    > $_.trim() ; $moveList = $moveList -replace $_ }
    > $moveList = $moveList -match "^\w+"
    > $moveList | out-file -encoding ASCII .\CleanUserList.txt
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com

    >> a
    >> b
    >> So the problem that I'm having is that in $_ is "userA" and it's
    >> matching both "userA" and "userAA". I only want it to match "userA".
    >> -----Original Message-----
    >>

    >>> From: Shay Levi [mailto:no@xxxxxx]
    >>> Posted At: Monday, January 28, 2008 5:37 PM
    >>> Posted To: microsoft.public.windows.powershell
    >>> Conversation: Using RegEx to get an exact match
    >>> Subject: Re: Using RegEx to get an exact match
    >>> Not sure I'm following you. Based on FileA and FileB, What would be
    >>> the
    >>> content
    >>> of CleanUserList.txt?
    >>> -----
    >>> Shay Levi
    >>> $cript Fanatic
    >>> http://scriptolog.blogspot.com
    >>> I'm working on a PowerShell script that will clean up a file that
    >>> has
    >>> a list of names to be moved to Exchange 2007 based off an exclusion
    >>> list. This is because I have some mailboxes that I don't want moved
    >>> (for one reason or another). So what I want to do is take fileA and
    >>> search through it for any user names in fileB and pull them out and
    >>> then save the results as a "clean" file.
    >>> The problem I've run into is that when using the -replace regex ,
    >>> it's not performing exact matches (at least as I had expected it
    >>> to). For example:
    >>>
    >>> fileA:
    >>> userA
    >>> userAA
    >>> userB
    >>> userBB
    >>> fileB:
    >>> userA
    >>> userB
    >>> My script looks like this:
    >>> #####################
    >>> # Cleaning up the list so all entries are sorted, unique, and in
    >>> lower
    >>> case
    >>> $moveList = get-content fileA.txt | sort-object -uniq |
    >>> foreach-object
    >>> {
    >>> $_.ToLower() }
    >>> # Cleaning up the list so all entries are sorted, unique, in lower
    >>> case,
    >>> and if they put in the domain portion of the account
    >>> # I strip that out.
    >>> $exclude = get-content fileB.txt | sort-object -uniq |
    >>> foreach-object
    >>> {
    >>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    >>> # Go through each object in $exclude and remove any matching object
    >>> in
    >>> $moveList
    >>> $exclude | foreach-object { $moveList = $moveList -replace $_ }
    >>> # Clean up whitespace left by replace operation
    >>> $moveList = $moveList -match "^\w+"
    >>> # Output clean file to file to be used as an answer file for mailbox
    >>> moves.
    >>> $moveList | out-file -encoding ASCII CleanUserList.txt
    >>> #####################
    >>> The problem that I'm seeing is that "$moveList -replace $_" is not
    >>> hitting exact matches. So in the above example if userA was in $_,
    >>> it
    >>> is matching:
    >>> userA
    >>> userAA
    >>> What I'm left with is "A" because it is stripping out the "userA"
    >>> part. How can I have it match only "userA" and not userAA" or better
    >>> yet, does anyone have a better way doing what I'm doing?
    >>> Thanks for any suggestions..as always I really appreciate it.


      My System SpecsSystem Spec

  6. #6


    Steven Guest

    Re: Using RegEx to get an exact match

    I think there's some confusion :-). Let me change to include more
    realistic names.

    fileA.txt:
    blee
    c_tlee
    ulee
    lee

    fileB.txt:
    c_smith
    lee
    radams

    In fileB one of the entries is "lee". This will match all 3 entries in
    fileA.txt and with the -replace operator is results in:
    b
    c_t
    u

    Remember that what I'm trying to do is take a file that has a list of
    user names and remove the ones that are in fileB from fileA. The above
    ends up corrupting the user name.which isn't what I'm trying to do :-).
    What the results should look like (if I can get this to work) is:
    blee
    c_tlee
    ulee

    Where it just removes "lee".
    _____________________________________________
    From: Shay Levi [mailto:no@xxxxxx]
    Posted At: Monday, January 28, 2008 6:48 PM
    Posted To: microsoft.public.windows.powershell
    Conversation: Using RegEx to get an exact match
    Subject: Re: Using RegEx to get an exact match


    Modified version:

    $moveList = get-content fileA.txt | sort -unique
    get-content fileB.txt | sort -unique | foreach { $moveList = $moveList
    -replace
    $_ }
    $moveList | where {$_ -ne ""} | out-file -encoding ASCII
    ..\CleanUserList.txt

    A
    B

    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I'm sure there's a shorter way , until then:
    >
    > $moveList = get-content fileA.txt | sort-object -uniq | foreach-object
    > {
    > $_.trim() }
    > $exclude = get-content fileB.txt | sort-object -uniq | foreach-object
    > {
    > $_.trim() ; $moveList = $moveList -replace $_ }
    > $moveList = $moveList -match "^\w+"
    > $moveList | out-file -encoding ASCII .\CleanUserList.txt
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com

    >> a
    >> b
    >> So the problem that I'm having is that in $_ is "userA" and it's
    >> matching both "userA" and "userAA". I only want it to match "userA".
    >> -----Original Message-----
    >>

    >>> From: Shay Levi [mailto:no@xxxxxx]
    >>> Posted At: Monday, January 28, 2008 5:37 PM
    >>> Posted To: microsoft.public.windows.powershell
    >>> Conversation: Using RegEx to get an exact match
    >>> Subject: Re: Using RegEx to get an exact match
    >>> Not sure I'm following you. Based on FileA and FileB, What would be
    >>> the
    >>> content
    >>> of CleanUserList.txt?
    >>> -----
    >>> Shay Levi
    >>> $cript Fanatic
    >>> http://scriptolog.blogspot.com
    >>> I'm working on a PowerShell script that will clean up a file that
    >>> has
    >>> a list of names to be moved to Exchange 2007 based off an exclusion
    >>> list. This is because I have some mailboxes that I don't want moved
    >>> (for one reason or another). So what I want to do is take fileA and
    >>> search through it for any user names in fileB and pull them out and
    >>> then save the results as a "clean" file.
    >>> The problem I've run into is that when using the -replace regex ,
    >>> it's not performing exact matches (at least as I had expected it
    >>> to). For example:
    >>>
    >>> fileA:
    >>> userA
    >>> userAA
    >>> userB
    >>> userBB
    >>> fileB:
    >>> userA
    >>> userB
    >>> My script looks like this:
    >>> #####################
    >>> # Cleaning up the list so all entries are sorted, unique, and in
    >>> lower
    >>> case
    >>> $moveList = get-content fileA.txt | sort-object -uniq |
    >>> foreach-object
    >>> {
    >>> $_.ToLower() }
    >>> # Cleaning up the list so all entries are sorted, unique, in lower
    >>> case,
    >>> and if they put in the domain portion of the account
    >>> # I strip that out.
    >>> $exclude = get-content fileB.txt | sort-object -uniq |
    >>> foreach-object
    >>> {
    >>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    >>> # Go through each object in $exclude and remove any matching object
    >>> in
    >>> $moveList
    >>> $exclude | foreach-object { $moveList = $moveList -replace $_ }
    >>> # Clean up whitespace left by replace operation
    >>> $moveList = $moveList -match "^\w+"
    >>> # Output clean file to file to be used as an answer file for mailbox
    >>> moves.
    >>> $moveList | out-file -encoding ASCII CleanUserList.txt
    >>> #####################
    >>> The problem that I'm seeing is that "$moveList -replace $_" is not
    >>> hitting exact matches. So in the above example if userA was in $_,
    >>> it
    >>> is matching:
    >>> userA
    >>> userAA
    >>> What I'm left with is "A" because it is stripping out the "userA"
    >>> part. How can I have it match only "userA" and not userAA" or better
    >>> yet, does anyone have a better way doing what I'm doing?
    >>> Thanks for any suggestions..as always I really appreciate it.


      My System SpecsSystem Spec

  7. #7


    Shay Levi Guest

    Re: Using RegEx to get an exact match



    $moveList = get-content fileA.txt | sort -unique
    get-content fileB.txt | sort -unique | foreach { $moveList = $moveList -replace
    "^$_`$" }
    $moveList | where {$_} | out-file -encoding ASCII .\CleanUserList.txt


    ### CleanUserList.txt ###
    blee
    c_tlee
    ulee



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I think there's some confusion :-). Let me change to include more
    > realistic names.
    >
    > fileA.txt:
    > blee
    > c_tlee
    > ulee
    > lee
    > fileB.txt:
    > c_smith
    > lee
    > radams
    > In fileB one of the entries is "lee". This will match all 3 entries
    > in
    > fileA.txt and with the -replace operator is results in:
    > b
    > c_t
    > u
    > Remember that what I'm trying to do is take a file that has a list of
    > user names and remove the ones that are in fileB from fileA. The
    > above
    > ends up corrupting the user name.which isn't what I'm trying to do
    > :-).
    > What the results should look like (if I can get this to work) is:
    > blee
    > c_tlee
    > ulee
    > Where it just removes "lee".
    > _____________________________________________
    > From: Shay Levi [mailto:no@xxxxxx]
    > Posted At: Monday, January 28, 2008 6:48 PM
    > Posted To: microsoft.public.windows.powershell
    > Conversation: Using RegEx to get an exact match
    > Subject: Re: Using RegEx to get an exact match
    > Modified version:
    >
    > $moveList = get-content fileA.txt | sort -unique
    > get-content fileB.txt | sort -unique | foreach { $moveList = $moveList
    > -replace
    > $_ }
    > $moveList | where {$_ -ne ""} | out-file -encoding ASCII
    > .\CleanUserList.txt
    > A
    > B
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com

    >> I'm sure there's a shorter way , until then:
    >>
    >> $moveList = get-content fileA.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.trim() }
    >> $exclude = get-content fileB.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.trim() ; $moveList = $moveList -replace $_ }
    >> $moveList = $moveList -match "^\w+"
    >> $moveList | out-file -encoding ASCII .\CleanUserList.txt
    >> -----
    >> Shay Levi
    >> $cript Fanatic
    >> http://scriptolog.blogspot.com

    >>> a
    >>> b
    >>> So the problem that I'm having is that in $_ is "userA" and it's
    >>> matching both "userA" and "userAA". I only want it to match
    >>> "userA".
    >>> -----Original Message-----
    >>>> From: Shay Levi [mailto:no@xxxxxx]
    >>>> Posted At: Monday, January 28, 2008 5:37 PM
    >>>> Posted To: microsoft.public.windows.powershell
    >>>> Conversation: Using RegEx to get an exact match
    >>>> Subject: Re: Using RegEx to get an exact match
    >>>> Not sure I'm following you. Based on FileA and FileB, What would be
    >>>> the
    >>>> content
    >>>> of CleanUserList.txt?
    >>>> -----
    >>>> Shay Levi
    >>>> $cript Fanatic
    >>>> http://scriptolog.blogspot.com
    >>>> I'm working on a PowerShell script that will clean up a file that
    >>>> has
    >>>> a list of names to be moved to Exchange 2007 based off an exclusion
    >>>> list. This is because I have some mailboxes that I don't want moved
    >>>> (for one reason or another). So what I want to do is take fileA
    >>>> and
    >>>> search through it for any user names in fileB and pull them out and
    >>>> then save the results as a "clean" file.
    >>>> The problem I've run into is that when using the -replace regex ,
    >>>> it's not performing exact matches (at least as I had expected it
    >>>> to). For example:
    >>>> fileA:
    >>>> userA
    >>>> userAA
    >>>> userB
    >>>> userBB
    >>>> fileB:
    >>>> userA
    >>>> userB
    >>>> My script looks like this:
    >>>> #####################
    >>>> # Cleaning up the list so all entries are sorted, unique, and in
    >>>> lower
    >>>> case
    >>>> $moveList = get-content fileA.txt | sort-object -uniq |
    >>>> foreach-object
    >>>> {
    >>>> $_.ToLower() }
    >>>> # Cleaning up the list so all entries are sorted, unique, in lower
    >>>> case,
    >>>> and if they put in the domain portion of the account
    >>>> # I strip that out.
    >>>> $exclude = get-content fileB.txt | sort-object -uniq |
    >>>> foreach-object
    >>>> {
    >>>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    >>>> # Go through each object in $exclude and remove any matching object
    >>>> in
    >>>> $moveList
    >>>> $exclude | foreach-object { $moveList = $moveList -replace $_ }
    >>>> # Clean up whitespace left by replace operation
    >>>> $moveList = $moveList -match "^\w+"
    >>>> # Output clean file to file to be used as an answer file for
    >>>> mailbox
    >>>> moves.
    >>>> $moveList | out-file -encoding ASCII CleanUserList.txt
    >>>> #####################
    >>>> The problem that I'm seeing is that "$moveList -replace $_" is not
    >>>> hitting exact matches. So in the above example if userA was in $_,
    >>>> it
    >>>> is matching:
    >>>> userA
    >>>> userAA
    >>>> What I'm left with is "A" because it is stripping out the "userA"
    >>>> part. How can I have it match only "userA" and not userAA" or
    >>>> better
    >>>> yet, does anyone have a better way doing what I'm doing?
    >>>> Thanks for any suggestions..as always I really appreciate it.


      My System SpecsSystem Spec

  8. #8


    Adam Guest

    Re: Using RegEx to get an exact match

    Re: Using RegEx to get an exact matchAnother method I was considering was a hash table.

    $hash = @{}
    gc exclude.txt | %{$hash.$_ = $_}
    gc fulllist.txt | %{if (-not $hash.containsvalue($_)) {$_}} > cleanlist.txt

    Cheers,

    Adam.
    "Steven" <evetsleep@xxxxxx> wrote in message news:000001c8627f$ee39e780$0202fea9@xxxxxx
    I think there's some confusion J. Let me change to include more realistic names.

    fileA.txt:

    blee

    c_tlee

    ulee

    lee


    fileB.txt:

    c_smith

    lee

    radams


    In fileB one of the entries is "lee". This will match all 3 entries in fileA.txt and with the -replace operator is results in:

    b

    c_t

    u

    Remember that what I'm trying to do is take a file that has a list of user names and remove the ones that are in fileB from fileA. The above ends up corrupting the user name.which isn't what I'm trying to do J. What the results should look like (if I can get this to work) is:

    blee

    c_tlee

    ulee


    Where it just removes "lee".

    _____________________________________________
    From: Shay Levi [mailto:no@xxxxxx]
    Posted At: Monday, January 28, 2008 6:48 PM
    Posted To: microsoft.public.windows.powershell
    Conversation: Using RegEx to get an exact match
    Subject: Re: Using RegEx to get an exact match


    Modified version:

    $moveList = get-content fileA.txt | sort -unique

    get-content fileB.txt | sort -unique | foreach { $moveList = $moveList -replace

    $_ }

    $moveList | where {$_ -ne ""} | out-file -encoding ASCII ..\CleanUserList.txt

    A

    B

    -----

    Shay Levi

    $cript Fanatic

    http://scriptolog.blogspot.com

    > I'm sure there's a shorter way , until then:

    >

    > $moveList = get-content fileA.txt | sort-object -uniq | foreach-object

    > {

    > $_.trim() }

    > $exclude = get-content fileB.txt | sort-object -uniq | foreach-object

    > {

    > $_.trim() ; $moveList = $moveList -replace $_ }

    > $moveList = $moveList -match "^\w+"

    > $moveList | out-file -encoding ASCII .\CleanUserList.txt

    > -----

    > Shay Levi

    > $cript Fanatic

    >> a

    >> b

    >> So the problem that I'm having is that in $_ is "userA" and it's

    >> matching both "userA" and "userAA". I only want it to match "userA".

    >> -----Original Message-----

    >>

    >>> From: Shay Levi [mailto:no@xxxxxx]

    >>> Posted At: Monday, January 28, 2008 5:37 PM

    >>> Posted To: microsoft.public.windows.powershell

    >>> Conversation: Using RegEx to get an exact match

    >>> Subject: Re: Using RegEx to get an exact match

    >>> Not sure I'm following you. Based on FileA and FileB, What would be

    >>> the

    >>> content

    >>> of CleanUserList.txt?

    >>> -----

    >>> Shay Levi

    >>> $cript Fanatic

    >>> I'm working on a PowerShell script that will clean up a file that

    >>> has

    >>> a list of names to be moved to Exchange 2007 based off an exclusion

    >>> list. This is because I have some mailboxes that I don't want moved

    >>> (for one reason or another). So what I want to do is take fileA and

    >>> search through it for any user names in fileB and pull them out and

    >>> then save the results as a "clean" file.

    >>> The problem I've run into is that when using the -replace regex ,

    >>> it's not performing exact matches (at least as I had expected it

    >>> to). For example:

    >>>

    >>> fileA:

    >>> userA

    >>> userAA

    >>> userB

    >>> userBB

    >>> fileB:

    >>> userA

    >>> userB

    >>> My script looks like this:

    >>> #####################

    >>> # Cleaning up the list so all entries are sorted, unique, and in

    >>> lower

    >>> case

    >>> $moveList = get-content fileA.txt | sort-object -uniq |

    >>> foreach-object

    >>> {

    >>> $_.ToLower() }

    >>> # Cleaning up the list so all entries are sorted, unique, in lower

    >>> case,

    >>> and if they put in the domain portion of the account

    >>> # I strip that out.

    >>> $exclude = get-content fileB.txt | sort-object -uniq |

    >>> foreach-object

    >>> {

    >>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}

    >>> # Go through each object in $exclude and remove any matching object

    >>> in

    >>> $moveList

    >>> $exclude | foreach-object { $moveList = $moveList -replace $_ }

    >>> # Clean up whitespace left by replace operation

    >>> $moveList = $moveList -match "^\w+"

    >>> # Output clean file to file to be used as an answer file for mailbox

    >>> moves.

    >>> $moveList | out-file -encoding ASCII CleanUserList.txt

    >>> #####################

    >>> The problem that I'm seeing is that "$moveList -replace $_" is not

    >>> hitting exact matches. So in the above example if userA was in $_,

    >>> it

    >>> is matching:

    >>> userA

    >>> userAA

    >>> What I'm left with is "A" because it is stripping out the "userA"

    >>> part. How can I have it match only "userA" and not userAA" or better

    >>> yet, does anyone have a better way doing what I'm doing?

    >>> Thanks for any suggestions..as always I really appreciate it.


      My System SpecsSystem Spec

  9. #9


    Shay Levi Guest

    Re: Using RegEx to get an exact match


    compare-object (get-content fileA.txt) (get-content fileB.txt) | where {
    $_.SideIndicator -eq "<=" } | foreach { $_.inputObject }

    blee
    c_tlee
    ulee


    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I think there's some confusion :-). Let me change to include more
    > realistic names.
    >
    > fileA.txt:
    > blee
    > c_tlee
    > ulee
    > lee
    > fileB.txt:
    > c_smith
    > lee
    > radams
    > In fileB one of the entries is "lee". This will match all 3 entries
    > in
    > fileA.txt and with the -replace operator is results in:
    > b
    > c_t
    > u
    > Remember that what I'm trying to do is take a file that has a list of
    > user names and remove the ones that are in fileB from fileA. The
    > above
    > ends up corrupting the user name.which isn't what I'm trying to do
    > :-).
    > What the results should look like (if I can get this to work) is:
    > blee
    > c_tlee
    > ulee
    > Where it just removes "lee".
    > _____________________________________________
    > From: Shay Levi [mailto:no@xxxxxx]
    > Posted At: Monday, January 28, 2008 6:48 PM
    > Posted To: microsoft.public.windows.powershell
    > Conversation: Using RegEx to get an exact match
    > Subject: Re: Using RegEx to get an exact match
    > Modified version:
    >
    > $moveList = get-content fileA.txt | sort -unique
    > get-content fileB.txt | sort -unique | foreach { $moveList = $moveList
    > -replace
    > $_ }
    > $moveList | where {$_ -ne ""} | out-file -encoding ASCII
    > .\CleanUserList.txt
    > A
    > B
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com

    >> I'm sure there's a shorter way , until then:
    >>
    >> $moveList = get-content fileA.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.trim() }
    >> $exclude = get-content fileB.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.trim() ; $moveList = $moveList -replace $_ }
    >> $moveList = $moveList -match "^\w+"
    >> $moveList | out-file -encoding ASCII .\CleanUserList.txt
    >> -----
    >> Shay Levi
    >> $cript Fanatic
    >> http://scriptolog.blogspot.com

    >>> a
    >>> b
    >>> So the problem that I'm having is that in $_ is "userA" and it's
    >>> matching both "userA" and "userAA". I only want it to match
    >>> "userA".
    >>> -----Original Message-----
    >>>> From: Shay Levi [mailto:no@xxxxxx]
    >>>> Posted At: Monday, January 28, 2008 5:37 PM
    >>>> Posted To: microsoft.public.windows.powershell
    >>>> Conversation: Using RegEx to get an exact match
    >>>> Subject: Re: Using RegEx to get an exact match
    >>>> Not sure I'm following you. Based on FileA and FileB, What would be
    >>>> the
    >>>> content
    >>>> of CleanUserList.txt?
    >>>> -----
    >>>> Shay Levi
    >>>> $cript Fanatic
    >>>> http://scriptolog.blogspot.com
    >>>> I'm working on a PowerShell script that will clean up a file that
    >>>> has
    >>>> a list of names to be moved to Exchange 2007 based off an exclusion
    >>>> list. This is because I have some mailboxes that I don't want moved
    >>>> (for one reason or another). So what I want to do is take fileA
    >>>> and
    >>>> search through it for any user names in fileB and pull them out and
    >>>> then save the results as a "clean" file.
    >>>> The problem I've run into is that when using the -replace regex ,
    >>>> it's not performing exact matches (at least as I had expected it
    >>>> to). For example:
    >>>> fileA:
    >>>> userA
    >>>> userAA
    >>>> userB
    >>>> userBB
    >>>> fileB:
    >>>> userA
    >>>> userB
    >>>> My script looks like this:
    >>>> #####################
    >>>> # Cleaning up the list so all entries are sorted, unique, and in
    >>>> lower
    >>>> case
    >>>> $moveList = get-content fileA.txt | sort-object -uniq |
    >>>> foreach-object
    >>>> {
    >>>> $_.ToLower() }
    >>>> # Cleaning up the list so all entries are sorted, unique, in lower
    >>>> case,
    >>>> and if they put in the domain portion of the account
    >>>> # I strip that out.
    >>>> $exclude = get-content fileB.txt | sort-object -uniq |
    >>>> foreach-object
    >>>> {
    >>>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    >>>> # Go through each object in $exclude and remove any matching object
    >>>> in
    >>>> $moveList
    >>>> $exclude | foreach-object { $moveList = $moveList -replace $_ }
    >>>> # Clean up whitespace left by replace operation
    >>>> $moveList = $moveList -match "^\w+"
    >>>> # Output clean file to file to be used as an answer file for
    >>>> mailbox
    >>>> moves.
    >>>> $moveList | out-file -encoding ASCII CleanUserList.txt
    >>>> #####################
    >>>> The problem that I'm seeing is that "$moveList -replace $_" is not
    >>>> hitting exact matches. So in the above example if userA was in $_,
    >>>> it
    >>>> is matching:
    >>>> userA
    >>>> userAA
    >>>> What I'm left with is "A" because it is stripping out the "userA"
    >>>> part. How can I have it match only "userA" and not userAA" or
    >>>> better
    >>>> yet, does anyone have a better way doing what I'm doing?
    >>>> Thanks for any suggestions..as always I really appreciate it.


      My System SpecsSystem Spec

  10. #10


    Steven Guest

    Re: Using RegEx to get an exact match

    This is exactly what I needed. I understand what ^$_ means (lines
    starts with $_), but what does `$ mean?

    ----------

    $moveList = get-content fileA.txt | sort -unique
    get-content fileB.txt | sort -unique | foreach { $moveList = $moveList
    -replace
    "^$_`$" }
    $moveList | where {$_} | out-file -encoding ASCII .\CleanUserList.txt


    ### CleanUserList.txt ###
    blee
    c_tlee
    ulee



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I think there's some confusion :-). Let me change to include more
    > realistic names.
    >
    > fileA.txt:
    > blee
    > c_tlee
    > ulee
    > lee
    > fileB.txt:
    > c_smith
    > lee
    > radams
    > In fileB one of the entries is "lee". This will match all 3 entries
    > in
    > fileA.txt and with the -replace operator is results in:
    > b
    > c_t
    > u
    > Remember that what I'm trying to do is take a file that has a list of
    > user names and remove the ones that are in fileB from fileA. The
    > above
    > ends up corrupting the user name.which isn't what I'm trying to do
    > :-).
    > What the results should look like (if I can get this to work) is:
    > blee
    > c_tlee
    > ulee
    > Where it just removes "lee".
    > _____________________________________________
    > From: Shay Levi [mailto:no@xxxxxx]
    > Posted At: Monday, January 28, 2008 6:48 PM
    > Posted To: microsoft.public.windows.powershell
    > Conversation: Using RegEx to get an exact match
    > Subject: Re: Using RegEx to get an exact match
    > Modified version:
    >
    > $moveList = get-content fileA.txt | sort -unique
    > get-content fileB.txt | sort -unique | foreach { $moveList = $moveList
    > -replace
    > $_ }
    > $moveList | where {$_ -ne ""} | out-file -encoding ASCII
    > .\CleanUserList.txt
    > A
    > B
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com

    >> I'm sure there's a shorter way , until then:
    >>
    >> $moveList = get-content fileA.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.trim() }
    >> $exclude = get-content fileB.txt | sort-object -uniq |
    >> foreach-object
    >> {
    >> $_.trim() ; $moveList = $moveList -replace $_ }
    >> $moveList = $moveList -match "^\w+"
    >> $moveList | out-file -encoding ASCII .\CleanUserList.txt
    >> -----
    >> Shay Levi
    >> $cript Fanatic
    >> http://scriptolog.blogspot.com

    >>> a
    >>> b
    >>> So the problem that I'm having is that in $_ is "userA" and it's
    >>> matching both "userA" and "userAA". I only want it to match
    >>> "userA".
    >>> -----Original Message-----
    >>>> From: Shay Levi [mailto:no@xxxxxx]
    >>>> Posted At: Monday, January 28, 2008 5:37 PM
    >>>> Posted To: microsoft.public.windows.powershell
    >>>> Conversation: Using RegEx to get an exact match
    >>>> Subject: Re: Using RegEx to get an exact match
    >>>> Not sure I'm following you. Based on FileA and FileB, What would be
    >>>> the
    >>>> content
    >>>> of CleanUserList.txt?
    >>>> -----
    >>>> Shay Levi
    >>>> $cript Fanatic
    >>>> http://scriptolog.blogspot.com
    >>>> I'm working on a PowerShell script that will clean up a file that
    >>>> has
    >>>> a list of names to be moved to Exchange 2007 based off an exclusion
    >>>> list. This is because I have some mailboxes that I don't want moved
    >>>> (for one reason or another). So what I want to do is take fileA
    >>>> and
    >>>> search through it for any user names in fileB and pull them out and
    >>>> then save the results as a "clean" file.
    >>>> The problem I've run into is that when using the -replace regex ,
    >>>> it's not performing exact matches (at least as I had expected it
    >>>> to). For example:
    >>>> fileA:
    >>>> userA
    >>>> userAA
    >>>> userB
    >>>> userBB
    >>>> fileB:
    >>>> userA
    >>>> userB
    >>>> My script looks like this:
    >>>> #####################
    >>>> # Cleaning up the list so all entries are sorted, unique, and in
    >>>> lower
    >>>> case
    >>>> $moveList = get-content fileA.txt | sort-object -uniq |
    >>>> foreach-object
    >>>> {
    >>>> $_.ToLower() }
    >>>> # Cleaning up the list so all entries are sorted, unique, in lower
    >>>> case,
    >>>> and if they put in the domain portion of the account
    >>>> # I strip that out.
    >>>> $exclude = get-content fileB.txt | sort-object -uniq |
    >>>> foreach-object
    >>>> {
    >>>> $_.ToLower() } | foreach-object { $_ -replace "^\w+\\+"}
    >>>> # Go through each object in $exclude and remove any matching object
    >>>> in
    >>>> $moveList
    >>>> $exclude | foreach-object { $moveList = $moveList -replace $_ }
    >>>> # Clean up whitespace left by replace operation
    >>>> $moveList = $moveList -match "^\w+"
    >>>> # Output clean file to file to be used as an answer file for
    >>>> mailbox
    >>>> moves.
    >>>> $moveList | out-file -encoding ASCII CleanUserList.txt
    >>>> #####################
    >>>> The problem that I'm seeing is that "$moveList -replace $_" is not
    >>>> hitting exact matches. So in the above example if userA was in $_,
    >>>> it
    >>>> is matching:
    >>>> userA
    >>>> userAA
    >>>> What I'm left with is "A" because it is stripping out the "userA"
    >>>> part. How can I have it match only "userA" and not userAA" or
    >>>> better
    >>>> yet, does anyone have a better way doing what I'm doing?
    >>>> Thanks for any suggestions..as always I really appreciate it.


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Using RegEx to get an exact match

Similar Threads
Thread Thread Starter Forum Replies Last Post
locate array index with a -match or regex. Justin Rich PowerShell 2 11 Feb 2010
What's the exact meaning of on-link? hairui Vista General 0 10 Feb 2009
Regex - match between defined characters Kryten PowerShell 6 05 Aug 2008
Piping regex match to commands Klaus H. Probst PowerShell 2 24 Jul 2008
regex global match like Perl m//g John Cook PowerShell 10 13 Jul 2007