Solved

simple regex question

Posted on 2014-01-17
6
223 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular Expression Question 9 64
Reg Expression ^$ for BGP or in general 4 47
Using a PHP variable in an RLIKE regex 5 35
Understanding a java regular expression 11 67
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

914 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

18 Experts available now in Live!

Get 1:1 Help Now