I have two regex that I need to join into one as I am using the RegularExpressionAttribute in ASP.NET and it does not allow multiple instances.
How can I join the following two regex into one?
.*?@(?!.*?\.\.)[^@]+$
[\x00-\x7F]
the first one checks that there are not 2 consecutive dots in the domain part of an email and the second regex checks that all characters are ascii
I thought it might have been as easy as joining them together like (.*?@(?!.*?\.\.)[^@]+$)([\x00-\x7F])
but this does not work
Here is link to previous post relating to this problem
EDIT: I am decorating an string property of my viewmodel using reglarexpression attribute and this gets rendered into javascript using unobtrusive therefore it has to validate using javascript. I failed to mention this in my initial post
You can use:
^[\x00-\x7F]+?@(?!.*?\.\.)(?=[\x01-\x7F]+$)[^@]+$
You can just use this regex
^[\x00-\x7F-[@]]*?@(?!.*?\.\.)[\x00-\x7F-[@]]+$
Or, if you want to match at least 1 character before @
:
^[\x00-\x7F]+@(?!.*?\.\.)[\x00-\x7F-[@]]+$
Mind that [\x00-\x7F]
also includes @
symbol. In C# regex, we can subtract this from the range using -[@]
inside the character class.
And you do not need the anchors since you are using this in a RegularExpressionAttribute
, I believe.
Here is a demo on regexstorm.net, remove the second @
, and you will have a match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With