简体   繁体   中英

replacing strings with Regex.Replace()

i am sure thats an easy one but im missing something...

i have 2 strings:

String x = "ows__x05de__x05e1__x05e4__x05e8__x00"

String y = "ows__x05de__x05e1__x05e4__x05e8__x000"

the difference between the strings is the additional '0' in String y.

i have an XML document that has this 2 strings in it multiple times. i need to replace String x to "CustName" and String y to "CustNumber" clearly i cant use the .Replace() method because string x and string y are identical to this method. i have tryed to use Regex.Replace() :

string XMLdummy = "ows__x05de__x05e1__x05e4__x05e8__x00='custName....' ows__x05de__x05e1__x05e4__x05e8__x000='33346464...'";

Regex rx = new Regex("^ows__x05de__x05e1__x05e4__x05e8__x00{1,36}$");
string result = rx.Replace(XMLdummy, "CustName");

Regex rx2 = new Regex("^ows__x05de__x05e1__x05e4__x05e8__x000{1,37}$");
result = rx2.Replace(XMLdummy, "CustNumber");

System.Diagnostics.Debug.WriteLine(result);

nothing happen and the result is equal to XMLdummy original string.

The required result should be:

CustName='custName....' CustNumber='33346464...'

Change the order of replacing the strings, replace the string with more characters first and then the other string:

XMLdummy = XMLdummy.Replace(y, "CustNumber").Replace(x, "CustName");

This will prevent both types of strings to be replaced at one go, and effectively do what you want.

For a better explanation, consider the two strings:

The word is 'GRAY'

and

The word is 'GRAYSCALE'

Now, suppose you want to replace GRAY with FirstWord and GRAYSCALE with SecondWord .

If you replaced GRAY first, you would get:

The word is 'GRAY' -> The word is 'FirstWord'
The word is 'GRAYSCALE' -> The word is 'FirstWordScale'

On the other hand, if you changed the order of replacing of the two strings and used two steps, you would get the correct result:

The word is 'GRAY' -> The word is 'GRAY'
The word is 'GRAYSCALE' -> The word is 'SecondWord'
-------------
The word is 'GRAY' -> The word is 'FirstWord'
The word is 'SecondWord' -> The word is 'SecondWord'

Yes you right that you are using two string with same only in second "0" more so when you are going to change, it will change both string and replace CutomerName first string and CustomerName0 in second string.

I tried to reuse your code and come to the solution that you can use following code to change the string using Regex , use below code to do it.

 string XMLdummy = "ows__x05de__x05e1__x05e4__x05e8__x00='custName....'"+
                           "ows__x05de__x05e1__x05e4__x05e8__x000='33346464...'";

        string rx = @"^ows__x05de__x05e1__x05e4__x05e8__x00${0,35}";

        XMLdummy = Regex.Replace(XMLdummy, rx, "CustName");

        string rx2 = @"ows__x05de__x05e1__x05e4__x05e8__x000";

        XMLdummy = Regex.Replace(XMLdummy, rx2, "CustNumber");

        System.Diagnostics.Debug.WriteLine(XMLdummy);

Output: "CustName='custName....'CustNumber='33346464...'"

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM