Solved

Can anyone explain this email validation regex

Posted on 2014-02-04
5
311 Views
Last Modified: 2014-02-06
This regex is often used to validate email addresses:

\w+([-+!$%&*/=?{|}.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Open in new window


Can anyone break it down and explain exactly what it is doing?
0
Comment
Question by:purplesoup
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
stergium earned 500 total points
Comment Utility
hello. i use this site whenever i have that kind of questions. http://regex101.com/#python

    /\w+([-+!$%&*/=?{|}.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/

    \w+ match any word character [a-zA-Z0-9_]
    1st Capturing group ([-+!$%&*/=?{|}.']\w+)*
    @ matches the character @ literally
    \w+ match any word character [a-zA-Z0-9_]
    2nd Capturing group ([-.]\w+)*
    \. matches the character . literally
    \w+ match any word character [a-zA-Z0-9_]
    3rd Capturing group ([-.]\w+)*

Open in new window


the copy paste of it .
i hope that helps
0
 

Author Comment

by:purplesoup
Comment Utility
Thanks - I'm not clear about capturing groups - I just tried reading this

http://www.regular-expressions.info/named.html

but I couldn't link it to what you had - can you make it any clearer for me?

Sorry!
0
 
LVL 13

Expert Comment

by:stergium
Comment Utility
what part of this expression is not clear to you ?  please explain
0
 

Author Comment

by:purplesoup
Comment Utility
I didn't understand what capturing groups were, as I think I mentioned, and the initial link I looked up didn't seem to explain it very well, however I did some more searching and I think I have the hang of it.

This is what I made of it:

([-+!$%&*/=?{|}.']\w+)*

The bits between ( and ) are the group. The * at the end refers to zero or more characters, if it had + on the end it would be one or more.

So now looking at the contents of the group, we have

[-+!$%&*/=?{|}.']\w+

Well the \w+ at the end is easy enough - a word character, one or more times.

So what of

[-+!$%&*/=?{|}.']

?

This I believe refers to any one of these characters is ok.

So valid matches might be

(nothing)

since the capturing group has * at the end, zero characters are a valid match.

+a

the plus (+) character is one of the allowed characters, but it has to be followed by at least one word character (in this case "a")

&abcd

the ampersand (&) character is one of the allowed characters, and it must be followed by one or more word characters, "abcd" is therefore acceptable.

This wouldn't be allowed:

%&*

because only one of the special characters is allowed and it isn't followed by one or more word characters.

That was the sort of explanation I was looking for.
0
 
LVL 13

Expert Comment

by:stergium
Comment Utility
[-+!$%&*/=?{|}.']    -> one of these characters.  
The  link that posted , which is a reference to me also , explains/breaks every regular expresion .
If you feel that your are not satisfied with the answer , you can request the help of a moderator.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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

7 Experts available now in Live!

Get 1:1 Help Now