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

Literal characters

From: Using Regular Expressions

Video: Literal characters

We're going to begin learning the syntax that we need to write regular expressions by starting with the simplest match of all, a literal character. In other words, the letter A matches the letter A. To do this, we're going to be working with strings. Strings are just several characters that have been strung together. So let's say that we have the string car, which is in the double quotes there, and we have the regular expression car. /car/ matches car. /car/ also matches the first three letters of carnival. It doesn't match the entire expression; it just matches those first three letters.

Literal characters

We're going to begin learning the syntax that we need to write regular expressions by starting with the simplest match of all, a literal character. In other words, the letter A matches the letter A. To do this, we're going to be working with strings. Strings are just several characters that have been strung together. So let's say that we have the string car, which is in the double quotes there, and we have the regular expression car. /car/ matches car. /car/ also matches the first three letters of carnival. It doesn't match the entire expression; it just matches those first three letters.

It's very similar to searching inside a word processor. It's the simplest match there is. It just says look for literal car inside the string and tell me if you get a match or not. Now although it seems really simple, there are a couple of points that I want us to cover. The first is that these searches are case sensitive by default. That may not be the way your word processor works. So here, if we had capital CAR as a string, well lowercase car as an expression would not match it, because it's case sensitive by default.

Now my advice is to write all of your expressions as case sensitive because all engines support it all of the time, and you don't have to worry about putting it into case-insensitive mode. I think most people write them as case-sensitive expressions. When you get to character classes, we're actually going to learn how to write expressions which match both upper- and lowercase, which is basically the same thing as being case insensitive. Let's try it out. So in regexPal, let's just start by entering our test data here, car, and up here we'll put car. Notice that it highlights exactly that part of it.

If I had carnival, you see that it doesn't highlight the part that didn't match, so it's really indicating to me, with highlighting, exactly what part matches and what part doesn't. Notice that if I change it to Carnival capital, it doesn't match anymore. The case sensitivity does matter. I can click this box up here for case insensitive and now it does match again, because now the case doesn't matter anymore. Here case does matter. If we were to put a capital C here, well then it does match in that case and doesn't match in that case--again, unless that's turned on or not.

So that's how the case sensitivity works. Now I also want to mention that whitespace does matter. So for example if we have car matching car, this does not match. It's actually looking for those literal three characters in a row, and you kind of would expect that from your experience with a word processor. So having returns, things like that, don't match; it has to literally match those three characters, one after another. Now remember that there's also this Global mode up here. Let's talk a bit about. By default in regexPal, Global is turned on, but by default in regular expressions, it's not turned on.

That box is normally not checked. That's a modifier that regexPal is adding for us. Inside our tester, it won't make much of a difference, but let's understand what it is. In standard mode, or non-global mode, or non-global matching, the earliest, or leftmost match, is always preferred. So, as it's reading from left to right, when it comes to the first match, that's the one that it's going to prefer. So, for example, if we have the word pizzazz, zz would match the first set of Zs in pizzazz, not the second set. It's the leftmost set that's going to match.

In Global matching, it'll match all matches that are found throughout the text. So zz in Global mode would match both sets of Zs in pizzazz. Let's try that out. P-I-Z-Z-A-Z-Z. I've got the Global off. Let's type a zz up there. It found the first one. If I put a check there, and now it finds both of them. And it actually alternates the colors between yellow and blue to show me the difference between the different matches. So again, there's the single one and there's the second one. So we're starting to get some indication that the regex engine actually reads from left to right as it traverses through our string.

Let's take a closer look at that. Bear with me here if it seems a little obvious, because there are some points that I want to make. Let's say we have the string, "The cow, camel and cat communicated," and the regular expression we're going to use is /cat/. The first thing the regular expression engine does is it goes to the first character in the string and it says, does the t that's here match the first part of this regular expression? No, there's no match there. c and t are different, so it goes for the next character. Do c and h match? No it doesn't. And it keeps moving along until it gets to the c in cow. Then it says, Ah! I now have a match.

Maybe this is the beginning of a pattern that matches, let me see. So now, it goes to the next character and says o. Does o match the next part of the expression? No, it does not. So therefore, it now says, that's not a match, and it proceeds to start again, this time starting with the o. Does o match the c? No, it doesn't. Then it keeps going down the line. Now let's say we get to camel. Now it says ah, c, that's a match. Ah! a, that's a match too, and then it moves to the next letter and says oh, that's not a match.

All right, so now it's not a match anymore. It doesn't keep searching from the m; instead, what it does here is it backtracks to the a. You see why it does that? Just in case the pattern would've matched on the a, it goes back. So it didn't match starting with the c. "cam" was not a match. So now the next place it's going to try, it's going to try ame. So it's going to try those three letters. Those aren't a match, and it will keep going, and then it'll finally get to cat and it'll match the c, the a, and the t, and it will say a ll right, now I've got a match.

Now if we were in Global mode, then it would keep going and the next place it would try would be at the space. The space would be the very next thing that it would try, and then move along. The c matches but o doesn't, so then it starts trying again just like before, until finally it works its way along to cat, and then it match that for the second time. So I know that's overly simplistic, and you might have been able to guess a lot of that, but I wanted to point out especially that backtracking that it does, because that's going to be important later on. When we have complex expressions, it's going to do that backtracking like it did when it got to work camel.

So it's easy to see here, but it'll pay off when we start looking at more complex things later on. One important principle that also comes out of this that you need to understand is that regular expressions are eager. So keep that rule in mind: regular expressions are eager. They are eager to return a match to you. They want to return one as fast as possible. The concert for the earliest match being preferred is an important one. You'll here me mention eagerness several times throughout this training. It seems simple here, but when we start writing complex expressions, this eagerness is going to play a big role.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

59 video lessons · 11696 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.