转义字符的最佳方式
我需要转义这些字符:+-&|!(){}[]^"~*?:\
,在它们前面加上 \\
。 最好的方法是什么?我的第一个想法是使用替换,但这会搜索字符串以查找要替换的每个项目。 我认为必须有一种方法可以使用正则表达式一次完成所有操作。
I need to escape these characters: +-&|!(){}[]^"~*?:\
by preceding them with a \\
.
What is the best way to do this. My first thought was using replace, but that would search the string for each item to replace.
I'm thinking there must be a way to do it with regular expressions that would get all in one pass.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用正则表达式是可能的。最棘手的部分是正确转义特殊字符而不进入反斜杠地狱:
StringBuilder
解决方案Eric J.提到的很简单而且相当优雅。这是一种编码方法:It's possible with a regular expression. The trickiest part is correctly escaping the special characters without getting into backslash hell:
The
StringBuilder
solution mentioned by Eric J. is simple and quite elegant. Here's one way to code it:使用 StringBuilder 可能是比正则表达式更好的选择。这是支持这个想法的msdn帖子: Regex.Replace 与 String.Replace 与 StringBuilder.Replace
Using a StringBuilder would probably be a better option than regex. Here is an msdn post to support the idea: Regex.Replace vs String.Replace vs StringBuilder.Replace
C# 中的字符串是不可变的,这意味着每个 string.Replace() 都会创建原始字符串的一个新的、修改后的副本。
对于许多应用程序来说,这并不重要。不过,既然你问这个问题,我想你的情况可能是这样。
最有效的方法可能是使用 StringBuilder 来构建修改后的字符串。循环遍历源字符串一次,并在每个字符串位置附加字符或转义版本(如果适用)。使用 StringBuilder 构造函数分配初始内部缓冲区大小略大于源字符串。
大多数其他答案都提到的正则表达式对于这个特定的应用程序来说可能也非常有效,并且涉及的代码更少。然而,由于 RegEx 本质上必须应用通用解析逻辑,因此它不可能像根据您的特定需求调整的解决方案那么快。另外,在某些情况下(但可能不是这种情况)RegEx 可能会非常慢。请参阅
http://en.wikipedia.org/wiki/.NET_Framework_version_history#Common_Language_Runtime_。 28CLR.29
http://www.codinghorror.com/blog/2006/01/regex-performance .html
Strings are immutable in C#, meaning that every string.Replace() will create a new, modified copy of the original string.
For many applications that really will not matter. Since you're asking about it, though, I assume it may in your case.
The most efficient approach is probably to use a StringBuilder to build up your modified string. Loop through the source string once, and either append the character at each string position, or an escaped version, as applicable. Use the StringBuilder constructor that pre-allocates the initial internal buffer size to be slightly larger than the source string.
RegEx, which most other answers allude to, will probably also be quite efficient for this particular application and will involve less code. However, since RegEx must inherently apply generalized parsing logic, it cannot be quite as fast as a solution tuned to your specific need. Also, in some cases (probably not this one though) RegEx can be very slow. See
http://en.wikipedia.org/wiki/.NET_Framework_version_history#Common_Language_Runtime_.28CLR.29
http://www.codinghorror.com/blog/2006/01/regex-performance.html
做到这一点的最佳方法肯定是使用正则表达式(Regex)!
给出以下输出:
The best way to do this in surely using regular expressions (Regex) !
Gives the following output :
免责声明:如果这会导致您的应用程序出现性能问题,请阅读其他答案中关于不使用正则表达式的论点(例如,如果这是一个非常大的字符串,其中包含大量可转义字符的实例) 。但是,如果您选择正则表达式,下面将解释如何用 1 行代码来完成它。
其
Regex.Replace
您正在寻找的。您提供要搜索的正则表达式、输入和针对每次匹配运行的MatchEvaluator
。在您的情况下,您只需返回 String.Concat(@"\",match.Value) 。像这样的东西(
input
是你的字符串):DIsclaimer: Do read the arguments in other answers about not using regex if this will cause a performance problem for your application(For example, if this is a very big string with lots of instances of your escapable characters). However, if regex is your choice the below will explain how to do it in 1 line of code.
Its
Regex.Replace
that you're looking for. You supply a regular expression that you're searching for, the input and aMatchEvaluator
which runs for every match. In your case you just returnString.Concat(@"\",match.Value)
.Something like this(
input
is your string):