Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I join two regex into one?

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

like image 781
kurasa Avatar asked Apr 23 '15 07:04

kurasa


2 Answers

You can use:

^[\x00-\x7F]+?@(?!.*?\.\.)(?=[\x01-\x7F]+$)[^@]+$
like image 89
anubhava Avatar answered Sep 19 '22 17:09

anubhava


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.

like image 40
Wiktor Stribiżew Avatar answered Sep 19 '22 17:09

Wiktor Stribiżew