Solved

simple regex question

Posted on 2014-01-17
6
225 Views
Last Modified: 2014-01-27
With the 1000's of new TLDs coming shortly I'm not going to bother validating email addresses using old regular expressions, however I'm trying  to add a basic regex  to ensure a string has:

(at least one character)@(at least one character).(at least one character)

anyone know what that regular expression would look like?
0
Comment
Question by:tf2012
6 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39789786
\w+@\w+\.\w+

HTH,
Dan
0
 
LVL 21

Accepted Solution

by:
Mazdajai earned 500 total points
ID: 39790154
That would work but it will ignore something like 'abc-a.b_c@ab-c.com'.

A valid email address can have dash, dot and underscore.

[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39790366
(at least one character)@(at least one character).(at least one character)
.+\@.+\..+
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 39792768
For validation purposes, you may want to at least disallow space and subsequent @ characters, and also disallow other characters before and after the email address, if the email address is meant to be by itself such as in a form. Adjusting @ozo's pattern:
^[^\s@]+@[^\s@]+\.[^\s@]+$

Open in new window

There may be other characters that you want to exclude. This page discusses the allowed characters, and may be of interest if you want to complicate things further :-)
0
 
LVL 84

Expert Comment

by:ozo
ID: 39792892
space and @ characters are actually allowed when properly quoted, as discussed in TerryAtOpus's link, but a regular expression to properly handle the official RFC822 spec can get pretty complicated, and may not be worth using given that there are other ways of checking email addresses, and that there can be other issues with email addresses besides whether they have a valid format.
0
 
LVL 1

Author Closing Comment

by:tf2012
ID: 39811714
great thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now