1

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...'
1
  • 1
    Why can't you use a normal replace??!? Commented Apr 9, 2016 at 11:59

2 Answers 2

2

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'
Sign up to request clarification or add additional context in comments.

1 Comment

yeee...that is thinking out of the box. thank you @FARHAN! i will check it as an answer in a few minuets.
1

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...'"

enter image description here

1 Comment

thank you @Devendra, now i understand what was wrong with my regex pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.