how can I join two regex into one?
Asked Answered
S

2

6

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

Snowy answered 23/4, 2015 at 7:31 Comment(0)
E
3

You can use:

^[\x00-\x7F]+?@(?!.*?\.\.)(?=[\x01-\x7F]+$)[^@]+$
Eckert answered 23/4, 2015 at 7:35 Comment(5)
Your regex will also match 444@[email protected].Hookup
I remember you said look-arounds are expensive, didn't you? :)Hookup
I don't remember actually on what question did I made that statement. But in general yes if there is an alternative then avoid lookarounds. However OP already is using a lookahead (?!.*?\.\.)Eckert
thanks for your answer. unfortunately I can only pick one answer. I upvoted yoursSnowy
as above answer - my unit test passes but actually does not work on page..I'll edit my question and show what I meanSnowy
W
3

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.

Washday answered 23/4, 2015 at 7:40 Comment(1)
I fixed it as I forgot to add the anchors and the character class subtraction to the first range occurrence. But since you updated the question stating the need for the regex to work with javascript, I agree this won't work.Hookup

© 2022 - 2024 — McMap. All rights reserved.