0

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 IsMandatoryTextTextAreaFiles.vcr_RegularExpression=\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* .

So what to do now?

1 Answer 1

1

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: [email protected] 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+)*$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.