Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Matching email addresses

From: Using Regular Expressions

Video: Matching email addresses

In this movie, I want us to see how to write a regular expression that will match e-mail addresses. I want us to do this after we first talked about postal codes, because we are going to run into the same issues that we ran in to there. Now, we all know what a basic e-mail address looks like, right? It's the username for the person, followed by the at sign, followed by the domain where their account is located. And that will help route the e-mail to the correct domain, and then from there, it can be routed to the correct person. Let's try and write the basic regular expression for this, and then we will consider what exceptions and edge cases we need to take into account.

Matching email addresses

In this movie, I want us to see how to write a regular expression that will match e-mail addresses. I want us to do this after we first talked about postal codes, because we are going to run into the same issues that we ran in to there. Now, we all know what a basic e-mail address looks like, right? It's the username for the person, followed by the at sign, followed by the domain where their account is located. And that will help route the e-mail to the correct domain, and then from there, it can be routed to the correct person. Let's try and write the basic regular expression for this, and then we will consider what exceptions and edge cases we need to take into account.

So to start with, let's just start with a simple e-mail address, someone@somewhere.com. Alright, so how do we write a regular expression for that? We know we need to use our anchor tags; we've learned by now. We'll put in the multi-line anchors, so that we can have more than one line here. And let's write that the first character needs to be a word character, and there can be one or more of those, followed by an actual at sign, followed by one or more word characters, and then a literal dot -- don't forget to escape it, because it's not a wildcard -- and then at the end, .com; we can match that by saying that it's any word character, and that there can be three of them.

Now, this matches our first example pretty well. But what if we have that same thing -- what if we had someone@somewhere.co.uk. That's a valid and very common one as well. Countries just have two letters at the end. So obviously, we want to say that this can be two, comma, three, but we still didn't match it, because there is this period here. We have to allow for that as well. So instead of just saying that what can go inside this area here, instead of just saying that it's a word character, let's instead change it to say it's a word character, or it can be a period as well.

And that will make sure that somewhere.co gets validated. In reality, we can't use all word characters here. You can't have .00 at the end. It has to be a letter. So let's go ahead and specify that a little more, and say that it actually can be an uppercase letter or a lowercase letter, A to Z. That still matches both our cases. Here the Ws are correct. We can use digits, and we can use underscores. So this will match the basics. However, there are some edge cases that we do need to take into account.

Let's go ahead and make this into a character set. And in addition to just having letters, numbers, and underscore, it's also perfectly legal to have a dot, a percent sign, a plus sign, or a hyphen. We'd need to escape that one, just to make sure that it's clear that that is an escaped dash, and not a range character. Now, you may never have encountered an e-mail address that includes any of these. However, there is a standards body that sets these. So you can look out on the Internet, and find the standard for what an e-mail address is allowed to be, and then write your regular expression to match that standard.

Same thing in the last part here. The domain can actually have a period in it; it can always have a hyphen as well. So we have now written a pretty good one that matches most cases. It matches all of the country possibilities, it matches .edu, it matches .gov, it matches .mil, it matches .net; all these different possibilities that you're used to seeing as the trailing domain name will be allowed for. However, there are actually some domain names that are rare that you might not know about.

In addition to all the two letter country codes, and the three letter domain names that you're used to, there are also a number of other ones here in this list -- like museum and travel -- that are much longer. Some of them are four letters, but some of them are as much as six letters long, and these are constantly changing and being added to. Now, you may decide that this is good enough for your purposes, or you may decide that you want to allow four characters, or even six or seven characters here. Or maybe you want to make it unlimited, and say I don't care how many characters are at the end. That's really up to you as to what you are trying to do.

In all of these cases, it doesn't ensure that the domain actually exists. Now, I think this is a pretty good regular expression that will match most cases, and this is something like what I might use. I just want to show you that if you prefer, you can replace this with what's at the end, with something like this. And what this will do is actually check that it's either, it's a two letter country code, A to Z, or it's one of these other domains. So now we at least make sure that it is an actual domain. Typing in ggg for a domain name would not work.

Now personally, I think this can be a little bit tough to maintain, to go back to old programs, and old code that you've written before, and always be updating it with whatever the latest set of domain names are. So it's up to you. If you need this kind of precision, if you really need to make sure that it is a valid domain name, then you will want to plug something like this in. If not, you may just want to go with something simpler like this, that just ensures that it's properly formatted, even though they may not actually exist. It's the trade-off that we've seen a couple times now, where you're choosing between precision, and simplicity and maintainability.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

59 video lessons · 11705 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 2m 18s
    1. Welcome
      56s
    2. Using the exercise files
      1m 22s
  2. 19m 55s
    1. What are regular expressions?
      3m 20s
    2. The history of regular expressions
      6m 40s
    3. Regular expression engines
      2m 44s
    4. Installing an engine
      4m 5s
    5. Notation conventions and modes
      3m 6s
  3. 21m 23s
    1. Literal characters
      6m 39s
    2. Metacharacters
      2m 1s
    3. The wildcard metacharacter
      4m 31s
    4. Escaping metacharacters
      4m 53s
    5. Other special characters
      3m 19s
  4. 31m 26s
    1. Defining a character set
      5m 49s
    2. Character ranges
      4m 49s
    3. Negative character sets
      4m 53s
    4. Metacharacters inside character sets
      5m 12s
    5. Shorthand character sets
      6m 30s
    6. POSIX bracket expressions
      4m 13s
  5. 36m 38s
    1. Repetition metacharacters
      7m 17s
    2. Quantified repetition
      6m 59s
    3. Greedy expressions
      6m 27s
    4. Lazy expressions
      6m 46s
    5. Using repetition efficiently
      9m 9s
  6. 20m 24s
    1. Grouping metacharacters
      4m 14s
    2. Alternation metacharacter
      4m 54s
    3. Writing logical and efficient alternations
      7m 33s
    4. Repeating and nesting alternations
      3m 43s
  7. 19m 19s
    1. Start and end anchors
      7m 21s
    2. Line breaks and Multiline mode
      4m 41s
    3. Word boundaries
      7m 17s
  8. 23m 33s
    1. Backreferences
      8m 57s
    2. Backreferences to optional expressions
      3m 51s
    3. Finding and replacing using backreferences
      7m 16s
    4. Non-capturing group expressions
      3m 29s
  9. 32m 31s
    1. Positive lookahead assertions
      6m 39s
    2. Double-testing with lookahead assertions
      7m 16s
    3. Negative lookahead assertions
      6m 10s
    4. Lookbehind assertions
      6m 26s
    5. The power of positions
      6m 0s
  10. 13m 13s
    1. About Unicode
      4m 19s
    2. Unicode in regular expressions
      4m 41s
    3. Unicode wildcards and properties
      4m 13s
  11. 1h 55m
    1. How to use this chapter
      5m 38s
    2. Matching names
      6m 33s
    3. Matching postal codes
      8m 54s
    4. Matching email addresses
      5m 0s
    5. Matching URLs
      8m 1s
    6. Matching decimal numbers and currency
      6m 45s
    7. Matching IP addresses
      7m 10s
    8. Matching dates
      7m 49s
    9. Matching times
      8m 59s
    10. Matching HTML tags
      8m 34s
    11. Matching passwords
      6m 49s
    12. Matching credit card numbers
      9m 36s
    13. Finding words near other words
      6m 38s
    14. Formatting with Search and Replace, pt. 1
      7m 22s
    15. Formatting with Search and Replace, pt. 2
      4m 15s
    16. Formatting with Search and Replace, pt. 3
      7m 10s
  12. 47s
    1. Goodbye
      47s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Using Regular Expressions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.