Solved

Can anyone explain this email validation regex

Posted on 2014-02-04
5
315 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
ID: 39832648
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
ID: 39832926
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
ID: 39838243
what part of this expression is not clear to you ?  please explain
0
 

Author Comment

by:purplesoup
ID: 39838411
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
ID: 39838686
[-+!$%&*/=?{|}.']    -> 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pattern problem 9 80
Python Regex Problem 24 135
edit mobile phone number in office 365 15 65
Grep - regex - find files matching pattern 2 55
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…
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…

777 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