Solved

What does the regular expression "[^\s@]+@[^\s@]+\.[^\s@]+" actually do?

Posted on 2014-01-13
2
331 Views
Last Modified: 2014-01-13
I'm trying to find a regex for email validation and someone suggested

[^\s@]+@[^\s@]+\.[^\s@]+

but looking up what the expression means here:

http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

It seems to be expecting lots of white space which doesn't make any sense.

Testing the expression it does seem fine for validating email addresses btw - I just can't understand how it is doing it.
0
Comment
Question by:purplesoup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 39776215
the ^ in the front of the \s@ part means NOT.
so, the expression above sais:

must start and have at least 1 non-space and non-@ character
then 1 @
continue and have at least 1 non-space and non-@ character
then 1 .
end and have at least 1 non-space and non-@ character
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 39776245
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr/[^\s@]+@[^\s@]+\.[^\s@]+/)->explain"
The regular expression:

(?-imsx:[^\s@]+@[^\s@]+\.[^\s@]+)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  [^\s@]+                  any character except: whitespace (\n, \r,
                           \t, \f, and " "), '@' (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  @                        '@'
----------------------------------------------------------------------
  [^\s@]+                  any character except: whitespace (\n, \r,
                           \t, \f, and " "), '@' (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  \.                       '.'
----------------------------------------------------------------------
  [^\s@]+                  any character except: whitespace (\n, \r,
                           \t, \f, and " "), '@' (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Title # Comments Views Activity
Specific format 21 240
c# regex: extract & replace text between braces 6 58
Need help decoding the following expressions 1 67
Regex HELP 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…
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…
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…
Suggested Courses

752 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