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

Start and end anchors

From: Using Regular Expressions

Video: Start and end anchors

In this chapter, we're going to be looking at anchored expressions, and we'll start out by looking at the most common kind of anchors, which are start and end anchors. The metacharacters we're going to need for this are the caret, the dollar sign, or the backslash A, and the backslash Z. Notice that the caret and the A, and the dollar sign and the Z, have very similar meanings. The only difference is about how it handles the difference between a string and a line. We'll talk more about that distinction in the next movie where we talk about multi-line mode. For now, let's just work with strings, and assume that they're basically the same.

Start and end anchors

In this chapter, we're going to be looking at anchored expressions, and we'll start out by looking at the most common kind of anchors, which are start and end anchors. The metacharacters we're going to need for this are the caret, the dollar sign, or the backslash A, and the backslash Z. Notice that the caret and the A, and the dollar sign and the Z, have very similar meanings. The only difference is about how it handles the difference between a string and a line. We'll talk more about that distinction in the next movie where we talk about multi-line mode. For now, let's just work with strings, and assume that they're basically the same.

Notice also that this is the second meaning we have for the caret. The first time we saw the caret was as a metacharacter, where it represented the negative character set, which is used only when it's the first character inside the square brackets. Here, it will have the anchor meaning when it's the first character in the regular expression. The dollar sign will have its meaning when it's the end of the regular expression, and the same for the A, and the Z. One important point about all anchors is that anchors refer to a position; not to an actual character.

We say that they are zero-width. Other expressions we've been working with have a width to them, because they refer to characters, and the regular expression engine expects them to match a certain number of characters. Here, instead what we're telling the regular expression engine is where to expect those characters to occur. Let's say that we have A, P, P, L, E, and in front of it we have the caret. What we're saying to our regex engine is, I'm not interested in you finding apple anywhere in the string. I'm only interested in it if you can find it at the beginning of the string; if it's the very first thing.

I'm requiring it to be in that position. It's the position; not an actual character. The second example there has the Dollar sign at the end, which says, I'm only interested in apple if it occurs at the end of the string. If it has apple somewhere in the middle, I don't care about it; it's not a match. Or, in the last case, we can put both and say, I've fully defined everything that ought to be in the string. If apple is not both the beginning and the end, then you don't have a match. So if we had a line that said apple sauce, then the first one would match, the second and the third one would not.

And then you can see on the right side, I've given you the versions of it using the capital A and the capital Z. Now, as far as support for these different metacharacters, the caret and the Dollar sign are supported in all regular expression engines. You can use them all the time. The backslash A and backslash Z, though, are not as widely supported. They're supported at a lot of places: Java, .NET, Perl, PHP, Python, Ruby; they all support it. But, for example, JavaScript does not. All the old UNIX tools do not. I've been burned by that a couple of times where I've tried to use the A and Z, thinking they were perfectly interchangeable, and they're not.

The caret and the dollar sign are definitely more universally accepted. I also find them a little easier to read. If you look at the examples there, where you've got a capital A sitting right next your words, I find that that's a little harder to read than the same version using the caret and the dollar sign. Let's try some examples. So let's start out with a simple phrase here: Mr. Smith went to Washington. For a regular expression to start with, let's just do capital A to Z as a character set. So you notice it found three of them; it found every capital letter that is in there.

But what if we really just want the first one? If we want the one at the beginning of the line, we put the caret in front. That essentially says, the only one that counts as a match is if the position of it is the beginning of the string. Notice that that's not the same thing as if we had a caret inside here; that's the negative character class in that case. Here it's the first character, and that let's us know it has to be at the beginning of the string. Let's try the opposite. Let's try a backslash, period, for a literal period. Now you see it found two of them.

Well, what if really want just the Period that's the end of the string? That's really what we're looking for is that tail end; we can use the Dollar sign at the end. Now it only finds one. It still parses along the whole string. When it gets to the first period after Mr, it then says, oh, is this the end of the string? No, it's not the end of the string. Therefore I don't have a match; better keep looking. And it keeps plowing its way along the string until it gets to the end, to the last period, and it says, is this one the end of the string? Yes, it is, and therefore we have a match. Let's try and write one real quick that would match everything here.

Let's do a dollar sign; let's say the first thing has to be a capital letter. Then after that, we'll make another character set that's a little more permissive. We'll put A to Z, uppercase or lowercase, it can also have a hyphen in it, and a period, and a space in it, and it can be repeated. So now we've written an expression that matches our entire string, and it has to match the full string from beginning to end, because we have those anchors on either side saying, this expression must match the whole thing. If, for example, I took out the period here, well now it doesn't match anymore, because it doesn't match this period here.

It doesn't match a part of it; it doesn't say I'm going to match a little bit. If I take that out, now it does match that little bit. I'm saying it has to match the entire string. Frequently, you use this type of full- string matching if you want to ensure that the content matches, and exactly matches. For example, let's say we were trying to do some kind of e-mail matching. We wanted to say nobody@nowhere.com. We want to write a regular expression that would match that from start to finish. I'm going to write a really, really simple one here. Let's just say any word character, followed by an at sign, followed by any word character, followed by a literal period, and then A to Z, three times.

And that's not going to match every e-mail address, but it's going to match a lot of them. It's good enough for our purposes. So, you can see it matches. But, for example, if I do comma, somebody@ somewhere.com, now I don't have a match. Now it doesn't match the string exactly from beginning to end. Now, if you were working with data where you actually knew you had two e-mail addresses in there, and you wanted to grab the first one, you could also use these anchors to make sure you got the first one, or make sure you grab the last one, and you could take those and do something with them.

But if we're really talking about matching -- this full matching -- then this makes sure that it matches exactly this expression, and nothing else. Let me show you another example. Let's try a find white space. Let's say we've got couple of spaces here; It was a dark and stormy night. If we want to find the leading white space -- white space that's the beginning -- maybe we want to be able to replace that; we'll learn how to do that later. Inside our character set repeated, you can say anything that's a tab or a space, and that will target anything that's a tab or a space at the beginning of our line.

And then we could potentially replace it, or do something else with it; turn it into tabs, or turn it into spaces. We could do the same thing at the end by using the dollar sign, removing that, and let's say then we have, and they lived happily ever after, period, space, space, space, space, space. See? It finds all that trailing white space at the end. So that's the fundamentals of working with these anchor metacharacters. They're pretty straightforward, but pretty powerful at the same time. In the next movie, we'll talk about how these anchor expressions work when you have line breaks in your text.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

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