[英]C# Regex remove ampersand
I have the following string: {}&{} and I want to remove ampersand to get {}{}. 我有以下字符串:{}&{},并且我想删除&符号以获取{} {}。 Here is my Regex.Replace call:
这是我的Regex.Replace呼叫:
Regex.Replace(@"\{\}&\{\}", @"\}.&\{", "}{")
I have no idea why it doesn't work. 我不知道为什么它不起作用。
Why complicating? 为什么要复杂化? why not just:
为什么不只是:
myString.Replace("}&{", "}{"); // replaces '}&{' with '}{'
What's the .
那是什么
.
doing there? 在那儿吗 That's going to match against any character, and since there is nothing between
}
and &
it will fail to match. 它将与任何字符匹配,并且由于
}
和&
之间没有任何匹配&
因此将无法匹配。 Try remove it: 尝试将其删除:
Regex.Replace(@"\{\}&\{\}", @"\}&\{", "}{")
Or make the character optional with ?
或使字符为可选
?
: :
Regex.Replace(@"\{\}&\{\}", @"\}.?&\{", "}{")
If you are not sure there is something between the ampersand and the curly bracket, but there could be something, add an asterisk after the period: 如果不确定与号和大括号之间是否存在某些内容,但可能存在某些内容,请在句号后添加一个星号:
Regex.Replace(@"\{\}&\{\}", @"\}.*&\{", "}{")
/* ^^ here */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.