Solved

simple regex question

Posted on 2014-01-17
6
228 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 35

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…

713 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