Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic email regex 1

Status
Not open for further replies.

Zhris

Programmer
Aug 5, 2008
254
GB
Hello,

I tried to create a very basic regular expression that will match an email address. Its meant to be as simple as possible, and shouldn't worry about the types of characters used. However its not working and i'm not sure why.

/^([^@]+@[^\.]+\.\w+(\.\w+)?){6,100}$/

-Match anything except the @ character
-Match an @ character
-Match anything except the . character
-Match a . character
-Match any number of letters
-If exists match a . character then any number of letters

Anyone tell me whats not right about the expression?

Thanks alot,

Chris
 
A quick scan: Shouldn't +@ need to escaped in the regex.
 
Hey,

Thanks for the reply,

I don't want to escape the + because I want its special meaning to be true (1 or more characters that aren't the @ symbol). I tried escaping the 2 @ symbols with no luck (although I didn't think @ symbols had a special meaning in regexes:

/^([^\@]+\@[^\.]+\.\w+(\.\w+)?){6,100}$/

Any other suggestions?

Chris
 
Try this:
Code:
/^[A-Za-z0-9._]+@[A-Za-z0-9.]+\.[A-Za-z]{2,3}$/
 
or maybe this
Code:
/^[^@]+@[^@]+\.[A-Za-z]{2,3}$/
but I'm not sure what characters are allowed for name and domain.
 
Hey,

Your regexes work great mikrom, and I may have to use 1.

My idea was to create a regex which isn't limited by what characters are used. For example, even though no such email exists I want to be able to match e.g. £*&&£$(ddd*£"@*&*£HJHdd.co.uk. The reason for this is I have had trouble in the past with users trying to register with email addresses containing characters which I never knew were allowed. I wanted to create a regex as loose as possible.

I managed to narrow down the problem regarding my original regex:

This DOES NOT work:
/^([^@]+@[^\.]+\.\w+(\.\w+)?){6,100}$/

This DOES work:
/^([^@]+@[^\.]+\.\w+(\.\w+)?)$/

When I remove the string length limiters, it works fine. The email I am testing is "email@domain.com" and i'm still completely baffled as to why my original regex doesn't work. Its important that I limit the length of the string. Is it not right to limit the length in this way: /^(mainregex){6,100}$/?

Chris
 
I would rather proove the length of the email address separately.
In the regex I would only proove the length of country-id if it's 2 or 3 characters (as I showed above).
 
Thank you very much for your help and advise.

For now i'm going to use the regex you provided (with a couple of small changes) (/^[^@]+@[^\.]+\.[A-Za-z\.]{2,8}$/), and worry about the overall length separately.

Chris
 
Zhris said:
When I remove the string length limiters, it works fine.

I think this is where this misunderstanding lies... they are not string length limiters... they specify the minimum and maximum number of repetitions of the previous expression, which in this case is the bracketed expression for the entire email address. In other words, it was expect from 6 to 100 whole email addresses.

Annihilannic.
 
Your regex
Code:
/^[^@]+@[COLOR=red][^\.]+[/color]\.[A-Za-z\.]{2,8}$/
matches an email address like this
Code:
xaz@gm[COLOR=red]@[/color]il.com
which is IMHO invalid, because it shouldn't contain more than one @ character.
 
You should look at using Email::Valid .... much more effecient, and copes with pretty much all valid email addys :)
 
Hey,

Thanks for all your input.

Annihilannic - that was pretty much the answer I was looking for, I figured this out later on. It was a silly mistake on my behalf. Although I think mikrom had tried to explain that to me, but I didn't "comprehend" it at the time of reading (usually check the forum last thing of the day).

mikrom - I don't actually use the email address for anything other than logging the user in (I never send users emails, even once they register). It may not be good practice, however I just wanted to ensure they registered with a "handle" that is near enough to an email, without restricting the user too much, which my past regex had done. However your right, and i've incorporated your advise.

youradds - Thanks for the module suggestion. I will keep that in mind when I need to ensure an email is entirely valid.

The regex I am using now is:
/^[^@]{1,60}@[^@]{1,60}(\.\w{2,6}){1,4}$/

Thanks again, your help is valuable.

Chris
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top