开发者

Escape regex in .NET?

Essentially what i am doing is finding a piece of text in a textf开发者_运维问答ile. The text is user specified so before i stick it into my regex expression i need to escape it all so . are \. and that something like {0, 3} is the string literal instead of part of the regex expression. Is there a function i can use to escape it? if not what chatacters should i stick \ in front of to ensure a valid regex statement?


Use Regex.Escape method -

Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜