Windows Vista Forums

Reverse Find and Replace ?
  1. #1


    Patrick Guest

    Reverse Find and Replace ?

    Hi,

    I'm wanting to Reverse find and replace a particular character in a
    Powershell string. e.g. reverse find "b" in "abcabc" and replace with
    "d" to give "abcadc". Could someone please advise how this could be
    achieved ?



    Thanks,

    Patrick

      My System SpecsSystem Spec

  2. #2


    Patrick Guest

    Re: Reverse Find and Replace ?

    Sorry - also should have stated that just the first instance of a
    particular character should be replaced.

    On Jan 8, 11:23*am, Patrick <prynh...@newsgroup> wrote:

    > Hi,
    >
    > I'm wanting to Reverse find and replace a particular character in a
    > Powershell string. *e.g. reverse find "b" in "abcabc" and replace with
    > "d" to give "abcadc". *Could someone please advise how this could be
    > achieved ?
    >
    > Thanks,
    >
    > Patrick

      My System SpecsSystem Spec

  3. #3


    Robert Robelo Guest

    Re: Reverse Find and Replace ?

    Try these:

    $s = 'abcabcabcabc'
    $find = 'b'
    $replace = '_'

    # last instance
    $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"

    # first instance
    $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"

    --
    Robert

      My System SpecsSystem Spec

  4. #4


    Karl Mitschke Guest

    Re: Reverse Find and Replace ?

    Hello Patrick,

    > Sorry - also should have stated that just the first instance of a
    > particular character should be replaced.
    >
    > On Jan 8, 11:23 am, Patrick <prynh...@newsgroup> wrote:
    >

    >> Hi,
    >>
    >> I'm wanting to Reverse find and replace a particular character in a
    >> Powershell string. e.g. reverse find "b" in "abcabc" and replace
    >> with "d" to give "abcadc". Could someone please advise how this
    >> could be achieved ?
    >>
    >> Thanks,
    >>
    >> Patrick
    >>
    In your example it's the last instance of the character that is being replaced.

    Is that what you want?

    You can do this like this:
    $test = "abcabc"
    $newstring = $test.Substring(0,$test.lastindexof("b")) + "d" + $test.substring($test.lastindexof("b")+1
    ,$test.length - $test.lastindexof("b")-1)

    Or like this one-liner
    ("abcabc").Substring(0,("abcabc").lastindexof("b")) + "d" + ("abcabc").substring(("abcabc").lastindexof("b")+1
    ,("abcabc").length - ("abcabc").lastindexof("b")-1)

    Someone smarter than me will show up with a regex method, which I won't understand


    Karl
    http://unlockpowershell.wordpress.com/



      My System SpecsSystem Spec

  5. #5


    Patrick Guest

    Re: Reverse Find and Replace ?

    Thanks Robert. I see that $1 and $3 are the regexp matches either side
    of $find. I was able to get correct output for the values provided
    below. However, the following example does not appear to work with
    this code:

    $s = '123123123123'
    $find = '2'
    $replace = '9'

    $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
    $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"

    I incorrectly get "$193, and $193123123123" respectively. I presume
    that "2" is a special case (i.e. $2)

    Regards,

    Patrick


    On Jan 8, 11:58*am, "Robert Robelo" <Ki...@newsgroup>
    wrote:

    > Try these:
    >
    > $s = 'abcabcabcabc'
    > $find = 'b'
    > $replace = '_'
    >
    > # last instance
    > $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
    >
    > # first instance
    > $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"
    >
    > --
    > Robert

      My System SpecsSystem Spec

  6. #6


    Patrick Guest

    Re: Reverse Find and Replace ?

    Ah yes - I was meaning the first character in the reverse find, i.e.
    the last character :-)

      My System SpecsSystem Spec

  7. #7


    Patrick Guest

    Re: Reverse Find and Replace ?

    Actually, any combination of a string involving digits seems to break:

    $s = '789789789a9'
    $find = 'a'
    $replace = '9'

    etc

    On Jan 8, 12:09*pm, Patrick <prynh...@newsgroup> wrote:

    > Thanks Robert. I see that $1 and $3 are the regexp matches either side
    > of $find. *I was able to get correct output for the values provided
    > below. *However, the following example does not appear to work with
    > this code:
    >
    > $s = '123123123123'
    > $find = '2'
    > $replace = '9'
    >
    > $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
    > $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"
    >
    > I incorrectly get "$193, and $193123123123" respectively. *I presume
    > that "2" is a special case (i.e. $2)
    >
    > Regards,
    >
    > Patrick
    >
    > On Jan 8, 11:58*am, "Robert Robelo" <Ki...@newsgroup>
    > wrote:
    >

    > > Try these:
    >

    > > $s = 'abcabcabcabc'
    > > $find = 'b'
    > > $replace = '_'
    >

    > > # last instance
    > > $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
    >

    > > # first instance
    > > $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"
    >

    > > --
    > > Robert

      My System SpecsSystem Spec

  8. #8


    Tome Tanasovski Guest

    RE: Reverse Find and Replace ?

    I think you'll need to use a real regex for this:
    $string = "abcabc"
    $r = new-object System.Text.RegularExpressions.Regex
    ("b",[System.Text.RegularExpressions.RegexOptions]::RightToLeft)
    $r.replace($string,"d",1)


    "Patrick" wrote:

    > Hi,
    >
    > I'm wanting to Reverse find and replace a particular character in a
    > Powershell string. e.g. reverse find "b" in "abcabc" and replace with
    > "d" to give "abcadc". Could someone please advise how this could be
    > achieved ?
    >
    > Thanks,
    >
    > Patrick
    > .
    >

      My System SpecsSystem Spec

  9. #9


    Robert Robelo Guest

    Re: Reverse Find and Replace ?

    Right, to fix this Just isolate the variable names with braces:

    $s1 = 'abcabcabcabc'
    $find = 'b'; $replace = 'd'
    $s1 -replace "^([^$find]*)($find)(.*)$", "`${1}${replace}`${3}"
    $s1 -replace "^(.*)($find)(.*)$", "`${1}${replace}`${3}"

    $s2 = '123123123123'
    $find = '2'; $replace = '9'
    $s2 -replace "^([^$find]*)($find)(.*)$", "`${1}${replace}`${3}"
    $s2 -replace "^(.*)($find)(.*)$", "`${1}${replace}`${3}"

    $s3 = '789a789a789a9'
    $find = 'a'; $replace = '9'
    $s3 -replace "^([^$find]*)($find)(.*)$", "`${1}${replace}`${3}"
    $s3 -replace "^(.*)($find)(.*)$", "`${1}${replace}`${3}"

    --
    Robert

      My System SpecsSystem Spec

Reverse Find and Replace ? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
find in files and replace Dan Holmes PowerShell 2 25 Mar 2010
find/replace on file James VB Script 4 18 May 2009
Advanced find and replace using VBScript Matthew Schwarz VB Script 1 02 Dec 2008
Find and Replace Utility ? Talal Itani Vista General 4 07 Aug 2007
Find/Replace eric.eickhoff@sbcglobal.net PowerShell 5 28 Mar 2007