.NET正则表达式中需要转义哪些字符? [英] What characters need to be escaped in .NET Regex?
本文介绍了.NET正则表达式中需要转义哪些字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
不知道完整的字符集 - 但是我不会依赖知识,我不会把它编入代码。相反,我将使用 Regex.Escape
每当我想要一些我不确定的文字文字:
//实际上不这样做来检查遏制...这只是一个例子。
public bool RegexContains(string haystack,string needle)
{
Regex regex = new Regex(^。*+ Regex.Escape(needle)+。* $);
return regex.IsMatch(haystack);
}
In a .NET Regex
pattern, what special characters need to be escaped in order to be used literally?
解决方案
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屋!
查看全文