需要.NET正则表达式转义哪些字符? [英] What characters need to be escaped in .NET Regex?
本文介绍了需要.NET正则表达式转义哪些字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个.NET <一个href="http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.regex.aspx"><$c$c>Regex$c$c>被字面上的使用模式,特殊字符必须以转义是什么?
In a .NET Regex
pattern, what special characters need to be escaped in order to be used literally?
推荐答案
我不知道完整的字符集 - 但我不会依赖于知识,无论如何,我不会把它变成code。相反,我会用<一个href="http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.regex.escape.aspx"><$c$c>Regex.Escape$c$c>每当我想要一些文字文本,我是不知道:
I don't know the complete set of characters - but I wouldn't rely on the knowledge anyway, and I wouldn't put it into code. Instead, I would use Regex.Escape
whenever I wanted some literal text that I wasn't sure about:
// Don't actually do this to check containment... it's just a little example.
public bool RegexContains(string haystack, string needle)
{
Regex regex = new Regex("^.*" + Regex.Escape(needle) + ".*$");
return regex.IsMatch(haystack);
}
这篇关于需要.NET正则表达式转义哪些字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文