开发者

Escape regex in .NET?

开发者 https://www.devze.com 2023-04-09 21:01 出处:网络
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 t

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

精彩评论

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