.NET Framework - Reverse Find and Replace ?

Asked By Patrick
07-Jan-10 06:03 PM
Hi,

I am wanting to Reverse find and replace a particular character in a
Powershell string.  e.g. reverse find "b" in "abcabc" and replace with
achieved ?

Thanks,

Patrick
System.Text.RegularExpressions.Regex
(1)
RightToLeft
(1)
Aparticular
(1)
Abcabc
(1)
Abcadc
(1)
Sideof
(1)
Regex
(1)
  Patrick replied to Patrick
07-Jan-10 06:03 PM
Sorry - also should have stated that just the first instance of a
particular character should be replaced.
  Robert Robelo replied to Patrick
07-Jan-10 05:58 PM
VHJ5IHRoZXNlOg0KDQokcyA9ICdhYmNhYmNhYmNhYmMnDQokZmluZCA9ICdiJw0KJHJlcGxhY2Ug
PSAnXycNCg0KIyBsYXN0IGluc3RhbmNlDQokcyAtcmVwbGFjZSAiXiguKikoJGZpbmQpKC4qKSQi
LCAiYCQxJHJlcGxhY2VgJDMiDQoNCiMgZmlyc3QgaW5zdGFuY2UNCiRzIC1yZXBsYWNlICJeKFte
JGZpbmRdKikoJGZpbmQpKC4qKSQiLCAiYCQxJHJlcGxhY2VgJDMiDQoNCi0tIA0KUm9iZXJ0
  Patrick replied to Robert Robelo
07-Jan-10 07:26 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
  Patrick replied to Patrick
07-Jan-10 07:26 PM
Actually, any combination of a string involving digits seems to break:

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

etc
  Tome Tanasovski replied to Patrick
07-Jan-10 10:33 PM
I think you will 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)
  Robert Robelo replied to Patrick
07-Jan-10 10:55 PM
UmlnaHQsIHRvIGZpeCB0aGlzIEp1c3QgaXNvbGF0ZSB0aGUgdmFyaWFibGUgbmFtZXMgd2l0aCBi
cmFjZXM6DQoNCiRzMSA9ICdhYmNhYmNhYmNhYmMnDQokZmluZCA9ICdiJzsgJHJlcGxhY2UgPSAn
ZCcNCiRzMSAtcmVwbGFjZSAiXihbXiRmaW5kXSopKCRmaW5kKSguKikkIiwgImAkezF9JHtyZXBs
YWNlfWAkezN9Ig0KJHMxIC1yZXBsYWNlICJeKC4qKSgkZmluZCkoLiopJCIsICJgJHsxfSR7cmVw
bGFjZX1gJHszfSINCg0KJHMyID0gJzEyMzEyMzEyMzEyMycNCiRmaW5kID0gJzInOyAkcmVwbGFj
ZSA9ICc5Jw0KJHMyIC1yZXBsYWNlICJeKFteJGZpbmRdKikoJGZpbmQpKC4qKSQiLCAiYCR7MX0k
e3JlcGxhY2V9YCR7M30iDQokczIgLXJlcGxhY2UgIl4oLiopKCRmaW5kKSguKikkIiwgImAkezF9
JHtyZXBsYWNlfWAkezN9Ig0KDQokczMgPSAnNzg5YTc4OWE3ODlhOScNCiRmaW5kID0gJ2EnOyAk
cmVwbGFjZSA9ICc5Jw0KJHMzIC1yZXBsYWNlICJeKFteJGZpbmRdKikoJGZpbmQpKC4qKSQiLCAi
YCR7MX0ke3JlcGxhY2V9YCR7M30iDQokczMgLXJlcGxhY2UgIl4oLiopKCRmaW5kKSguKikkIiwg
ImAkezF9JHtyZXBsYWNlfWAkezN9Ig0KDQotLSANClJvYmVydA==
Create New Account
help
I Tried Regx , But i will Use your solution example below string sdt0 = "660810"; sdt0 = System.Text.RegularExpressions.Regex.Replace(sdt0, @"( \ d{2})( \ d{2})( \ d{2})", "$2 / $3 / $1"); Console.WriteLine(DateTime.Parse(sdt0)); Console.WriteLine(sdt0); / / MMDDYY string sdt1 = "081066"; sdt1 = System.Text.RegularExpressions Regex.Replace(sdt1, @"( \ d{2})( \ d{2})( \ d{2})", "$1 / $2 / $3"); Console.WriteLine(DateTime.Parse
habe folgendes Problem, Gegeben: Pattern = "([ \ + \ - \ * \ ^ \ ( \ ) \ ])" Formel = "3+15*sin(x)+41" Dim Token As New System.Text.RegularExpressions.Regex(Pattern) Dim StringToken As String() = Token.Split(Formel) StringToken Ausgabe: 3 + 15 * sin ( x ) + 41 d+ \ . \ d+ | \ w+ | \ + | \ - | \ * | \ / | \ | \ ( | \ )(?< = *)" Dim Formel = "3.14 + 15 *sin( x ) + 41" For Each match In System.Text.RegularExpressions.Regex.Matches(Formel, Pattern) Console.WriteLine("{0} ({1})", match, match.ToString.Length) Next / / / Nicht über die
CCY, 1Y, 2Y, 1M, 2M, 3M, 6M I use regular expression as below: column.ColumnName = System.Text.RegularExpressions.Regex.Replace(column.ColumnName, However, this give me a runtime error: "parsing \ "^IR(*) \ " - Quantifier {x, y World!"); foreach (String s in table) { System.Console.Write("Entry: " + s + " = > "); / / Bad: / / System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s, "^IR(*)", string.Empty)); / / Better: System.Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s
given to me, I cannot simply say " filename = "C: \ wutemp \ myfile.txt" " Dim replaceRegEx As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(fileName, getRegexOptions()) Dim findValue As String = " \ wutemp \ " Dim replaceValue As String = " \ newfolder \ " Dim newFileName As
given to me, I cannot simply say " filename = "C: \ wutemp \ myfile.txt" " Dim replaceRegEx As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(fileName, getRegexOptions()) Dim findValue As String = " \ wutemp \ " Dim replaceValue As String = " \ newfolder \ " Dim newFileName As