Reverse Find and Replace ?

  • Follow


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
0
Reply Patrick 1/7/2010 10:23:06 PM

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

On Jan 8, 11:23=A0am, Patrick <prynh...@gmail.com> wrote:
> Hi,
>
> I'm wanting to Reverse find and replace a particular character in a
> Powershell string. =A0e.g. reverse find "b" in "abcabc" and replace with
> "d" to give "abcadc". =A0Could someone please advise how this could be
> achieved ?
>
> Thanks,
>
> Patrick

0
Reply Patrick 1/7/2010 10:30:07 PM


VHJ5IHRoZXNlOg0KDQokcyA9ICdhYmNhYmNhYmNhYmMnDQokZmluZCA9ICdiJw0KJHJlcGxhY2Ug
PSAnXycNCg0KIyBsYXN0IGluc3RhbmNlDQokcyAtcmVwbGFjZSAiXiguKikoJGZpbmQpKC4qKSQi
LCAiYCQxJHJlcGxhY2VgJDMiDQoNCiMgZmlyc3QgaW5zdGFuY2UNCiRzIC1yZXBsYWNlICJeKFte
JGZpbmRdKikoJGZpbmQpKC4qKSQiLCAiYCQxJHJlcGxhY2VgJDMiDQoNCi0tIA0KUm9iZXJ0

0
Reply Robert 1/7/2010 10:58:52 PM

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 =3D '123123123123'
$find =3D '2'
$replace =3D '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=A0am, "Robert Robelo" <Ki...@HighPlainsDrifter.com>
wrote:
> Try these:
>
> $s =3D 'abcabcabcabc'
> $find =3D 'b'
> $replace =3D '_'
>
> # last instance
> $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
>
> # first instance
> $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"
>
> --
> Robert

0
Reply Patrick 1/7/2010 11:09:10 PM

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

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

etc

On Jan 8, 12:09=A0pm, Patrick <prynh...@gmail.com> wrote:
> Thanks Robert. I see that $1 and $3 are the regexp matches either side
> of $find. =A0I was able to get correct output for the values provided
> below. =A0However, the following example does not appear to work with
> this code:
>
> $s =3D '123123123123'
> $find =3D '2'
> $replace =3D '9'
>
> $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
> $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"
>
> I incorrectly get "$193, and $193123123123" respectively. =A0I presume
> that "2" is a special case (i.e. $2)
>
> Regards,
>
> Patrick
>
> On Jan 8, 11:58=A0am, "Robert Robelo" <Ki...@HighPlainsDrifter.com>
> wrote:
>
> > Try these:
>
> > $s =3D 'abcabcabcabc'
> > $find =3D 'b'
> > $replace =3D '_'
>
> > # last instance
> > $s -replace "^(.*)($find)(.*)$", "`$1$replace`$3"
>
> > # first instance
> > $s -replace "^([^$find]*)($find)(.*)$", "`$1$replace`$3"
>
> > --
> > Robert

0
Reply Patrick 1/7/2010 11:19:33 PM

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
> .
> 
0
Reply Utf 1/8/2010 3:33:01 AM

UmlnaHQsIHRvIGZpeCB0aGlzIEp1c3QgaXNvbGF0ZSB0aGUgdmFyaWFibGUgbmFtZXMgd2l0aCBi
cmFjZXM6DQoNCiRzMSA9ICdhYmNhYmNhYmNhYmMnDQokZmluZCA9ICdiJzsgJHJlcGxhY2UgPSAn
ZCcNCiRzMSAtcmVwbGFjZSAiXihbXiRmaW5kXSopKCRmaW5kKSguKikkIiwgImAkezF9JHtyZXBs
YWNlfWAkezN9Ig0KJHMxIC1yZXBsYWNlICJeKC4qKSgkZmluZCkoLiopJCIsICJgJHsxfSR7cmVw
bGFjZX1gJHszfSINCg0KJHMyID0gJzEyMzEyMzEyMzEyMycNCiRmaW5kID0gJzInOyAkcmVwbGFj
ZSA9ICc5Jw0KJHMyIC1yZXBsYWNlICJeKFteJGZpbmRdKikoJGZpbmQpKC4qKSQiLCAiYCR7MX0k
e3JlcGxhY2V9YCR7M30iDQokczIgLXJlcGxhY2UgIl4oLiopKCRmaW5kKSguKikkIiwgImAkezF9
JHtyZXBsYWNlfWAkezN9Ig0KDQokczMgPSAnNzg5YTc4OWE3ODlhOScNCiRmaW5kID0gJ2EnOyAk
cmVwbGFjZSA9ICc5Jw0KJHMzIC1yZXBsYWNlICJeKFteJGZpbmRdKikoJGZpbmQpKC4qKSQiLCAi
YCR7MX0ke3JlcGxhY2V9YCR7M30iDQokczMgLXJlcGxhY2UgIl4oLiopKCRmaW5kKSguKikkIiwg
ImAkezF9JHtyZXBsYWNlfWAkezN9Ig0KDQotLSANClJvYmVydA==

0
Reply Robert 1/8/2010 3:55:34 AM

6 Replies
969 Views

(page loaded in 0.134 seconds)

Similiar Articles:
















7/20/2012 5:26:23 PM


Reply: