Solved

simple regex question

Posted on 2014-01-17
6
219 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
Comment Utility
\w+@\w+\.\w+

HTH,
Dan
0
 
LVL 21

Accepted Solution

by:
Mazdajai earned 500 total points
Comment Utility
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
Comment Utility
(at least one character)@(at least one character).(at least one character)
.+\@.+\..+
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
Comment Utility
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
Comment Utility
great thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now