[英]regex replacement word not working as expected
I have an incomplete xml file that needs a quick fix. 我有一个不完整的xml文件,需要快速修复。 Simplified, here is how it is coming in:
简化了,这是它的来历:
<positions><date></date><cust></cust><date></date><cust></cust></positions>
Here is what it needs to look like to process downstream: 这是处理下游所需的外观:
</date><cust></cust></position><position><date></date><cust></cust></position></positions>
I thought this would work: 我认为这可以工作:
Regex r = new Regex(@"\b<date>\b");
findFirstTag = r.Replace(findFirstTag, "<position><date>");
Regex x = new Regex(@"\b</cust>\b");
findFirstTag = x.Replace(findFirstTag, "</cust></position>");
Console.WriteLine("Converted by regex: " + findFirstTag + "\n");
Nothing changed. 没有改变。 Anyone?
任何人?
You are using a word boundary , but there isn't one before the "<" or after the ">". 您使用的是单词边界 ,但是在“ <”或“>”之后没有一个。 A word boundary occurs on the change from a word character to a non word character or from a non word character to a word character.
从单词字符到非单词字符或从非单词字符到单词字符的变化会出现单词边界。 Here you want to have it between two non wordcharacters "><" this is always false.
在这里,您希望在两个非单词字符“> <”之间使用它,这始终是错误的。
Just remove it and you should be fine: 只需将其删除,就可以了:
Regex r = new Regex(@"<date>");
and 和
Regex x = new Regex(@"</cust>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.