开发者

asp.net mvc dynamic regular expression variable

i am dynamically validating regular expression in which the regular expression are from database

ok so the thing is happening that \ is replaced by \\ which is not validating the email

i have Regex RegExp = new Regex("@" + IsMandatoryTextTextAreaFiles.vcr_RegularExpression, RegexOptions.Compiled);

in which IsMandatoryText开发者_运维知识库TextAreaFiles.vcr_RegularExpression=\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* .

So what to do now?


No, the \ are not replaced by \\, my guess is that you are looking at the value in the debugger, where the string is displayed as it would be written as a string literal in the code.

Concatenating the string "@" and some other string doesn't make it a literal string, i.e. "@" + "asdf" is not the same as @"asdf", it's just "@asdf". That means that the @ becomes part of the pattern. Just use the string as it is:

Regex RegExp = new Regex(IsMandatoryTextTextAreaFiles.vcr_RegularExpression, RegexOptions.Compiled);

Note that the regular expression is only verifying that a string contains a valid email, not that it is only a valid email. So for example the string "Here: asdf@asdf.com dear sir" would pass the test.

You can use ^ and $ to specify the beginning and end of the string in the regular expression:

^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜