简体   繁体   English

用Regex.Replace()替换字符串

[英]replacing strings with Regex.Replace()

i am sure thats an easy one but im missing something... 我相信这很简单,但我错过了一些东西......

i have 2 strings: 我有2个字符串:

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. 字符串之间的差异是字符串y中的附加“0”。

i have an XML document that has this 2 strings in it multiple times. 我有一个XML文档,其中有两个字符串多次。 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. 我需要将String x替换为“CustName”,将String y替换为“CustNumber”,但我不能使用.Replace()方法,因为字符串x和字符串y与此方法相同。 i have tryed to use Regex.Replace() : 我试过使用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. 没有任何反应,结果等于XMLdummy原始字符串。

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 . 现在,假设你要替换GRAYFirstWordGRAYSCALESecondWord

If you replaced GRAY first, you would get: 如果你先取代GRAY ,你会得到:

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. 是的,你正确使用两个字符串只有在第二个“0”中更多,所以当你要改变时,它将改变两个字符串并在第二个字符串中替换CutomerName第一个字符串和CustomerName0。

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. 我试图重用你的代码并找到你可以使用以下代码使用Regex更改字符串的解决方案,使用下面的代码来执行它。

 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...'" 输出:“CustName ='custName ....'CustNumber ='33346464 ...'”

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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