Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Matching IP addresses


From:

Using Regular Expressions

with Kevin Skoglund

Video: Matching IP addresses

In this movie, we'll write a regular expression to match IP addresses, and in the process, we'll explore some important points about how regular expressions work with matching numbers. Let's take a look at how an IP address typically looks. It's typically made up of four different parts; each one is a number separated by a decimal, and those numbers can range between 0 and 255. So the lowest number we can have would be 0.0.0.0, and the highest number we can have will be 255.255.255.255. And it would be some number in between there.
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 27s
    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 31s
    6. POSIX bracket expressions
      4m 13s
  5. 36m 39s
    1. Repetition metacharacters
      7m 17s
    2. Quantified repetition
      6m 59s
    3. Greedy expressions
      6m 27s
    4. Lazy expressions
      6m 47s
    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 32s
    1. Positive lookahead assertions
      6m 39s
    2. Double-testing with lookahead assertions
      7m 16s
    3. Negative lookahead assertions
      6m 11s
    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

please wait ...
Watch the Online Video Course Using Regular Expressions
5h 36m Intermediate Nov 21, 2011

Viewers: in countries Watching now:

Learn how to find and manipulate text quickly and easily using regular expressions. Author Kevin Skoglund covers the basic syntax of regular expressions, shows how to create flexible matching patterns, and demonstrates how the regular expression engine parses text to find matches. The course also covers referring back to previous matches with backreferences and creating complex matching patterns with lookaround assertions, and explores the most common applications of regular expressions.

Topics include:
  • Creating flexible patterns using character sets
  • Achieving efficiency when using repetition
  • Understanding different types of search strategies
  • Writing logical and efficient alternations
  • Capturing groups and reusing them with backreferences
  • Developing complex patterns with lookaround assertions
  • Working with Unicode and multibyte characters
  • Matching email addresses, URLs, dates, HTML tags, and credit card numbers
  • Using search and replace to format a document
Subject:
Developer
Software:
Regular Expressions
Author:
Kevin Skoglund

Matching IP addresses

In this movie, we'll write a regular expression to match IP addresses, and in the process, we'll explore some important points about how regular expressions work with matching numbers. Let's take a look at how an IP address typically looks. It's typically made up of four different parts; each one is a number separated by a decimal, and those numbers can range between 0 and 255. So the lowest number we can have would be 0.0.0.0, and the highest number we can have will be 255.255.255.255. And it would be some number in between there.

Typically, it might look something like this: 67.52.159.38. You can also have leading zeros in front of there. It's perfectly acceptable to have 067.052.159.038. Often they're omitted, but let's not forget about this case. So that's pretty simple, right? Each of those four parts can be a number between 0 to 255. Let's take a stab by writing an expression for it, and see what problems we run into. So to begin with let's turn on multi-line anchors, since we're using multiple lines, and we'll just match a whole line by using our beginning and end of line anchors.

The simplest thing that we might try to begin with is just to say, well let's use a digit one or more times, followed by a literal decimal. Make sure that you escape it so that you get the literal decimal. Then I'll just copy that, and I'll paste that four times, and remove the last decimal there. So now we've got four digits with dots in between. Now, that definitely does match, but that's not so great, because it's pretty unlimited. I mean it matches, for example, 99999, with a whole bunch of things like that, still get matched as well. Now, you could come back, and you could limit these, and say, okay, it's going to be only three characters max, but we already know that there is the possibility it might be one to three characters.

So let's put that in there. That's a slight improvement. Now it doesn't match those, but it does still match 999, and 999. So that doesn't work for us. What we need is a way that we can actually tell it the number is from 0 to 255 each time. Now, you might be tempted to just take this digit here and say, well instead, let's just say that it's 0 to 255. That doesn't match most of our examples, and the reason why is because that's a character set.

Remember, the range that's in there is telling it to make a range between 0 and 2. So that's the literal characters 0, 1, and 2, followed by the literal characters 5, and 5. So the possibilities for that first digit now are 0, 1, 2 and 5; that's it. It's not a number range. It's not from 0 up to 255. Do you see the problem here? This is what I refer to as a number as string problem. Regular expressions treat numbers as strings. they don't have a concept about what numbers come next in a series.

It's just looking at it as if it was the letter Q, or the letter Z. So instead, what we have to do is find a way to write a regular expression that will match the text version of 255, and all possibilities that are below that. So let's just try it with the first one here, and then whatever we come up with there, we know we can repeat three other times. So for this first expression here, it's possible that first digit could be a 0, or 1, or 2. That's what's possible for that first digit. After that, the second digit could be a 0, could be a 5; it could be a 9, though.

After all, our number could be 199. So we need to allow all digits 0 to 9 here, followed by all digits 0 to 9 again. So we did match a few more things in that process. However, there's also a possibility that those are optional; that they get omitted. So in that case, we need to make both of those optional. So now we've matched our four IP addresses, but we still haven't solved our problem, and here's why. What if we instead changed this to 299.299.299.299? That matches as well. It's still above our 255 limit.

We've only solved the first digit. we haven't solved the rest of it. Now, rather than stumble through it until we arrive at the right solution, let's stop and take a methodical approach to how we can solve this. What we need to do is break down 255 into each of its parts. So, for example, we have the possibility that it's a number between 250 and 255. In that case, we know that the regular expression for it will be 2 and a 5, and the last digit would be 0 to 5; not 0 to 9. We have a very limiting factor there, because we don't want to accidentally get 256 in there.

So we want to make sure that we isolate only the numbers from 250 to 255. We also can then break down 200 to 249. Now, we've already taken care of 250 to 255, but we want to take care of everything up until then. But in those cases, we do want to allow it to go ahead and have 0 to 9 as that last digit. So it's basically the same thing we had before, but just now that last digit can be 0 to 9. And let's keep working our way down; if we go from 100 to 199, that would be 1 in the first place, followed by 0 to 9, followed by 0 to 9, and then of course, just dropping down to 000.

Then we have some optional parts. It could have one or two zeros in front of it, or it could just be a single number by itself. This will allow us to get all the way up to 99. Now, there is one point that I want to make here, which is that it is a slight optimization if we move that optional question mark from the second character set in that expression, to the last one. The reason why that's true is because of the way we saw earlier working with greediness. It's faster if we allow the regular expression engine to find an actual number first, and make anything that's optional come after that.

It reduces the amount of backtracking that it has to do. It's a minor technical point, but it is worthwhile to consider. Now, we can actually combine these last two lines. Everything from 000 up to 199 can be written as one single expression, and that's because those last digits on all of them are allowed to go all the way up to 9. So we can just combine them into the shorter version that says 01 -- that's optional; may or may not exist -- and then the next portion after that is really the numbers 0 to 99. So now we've broken it down into these parts, and figured out how to match each portion as a string. All we have to do is assemble this together by using alternation.

So here as our first element, instead what we had before, let's just paste in our new combined string. So you can see I've got 255, 0 to 5, or it's 200 to 249, or it's anything from 000 up to 199. Now it no longer matches that 299, and of course, we want to just repeat this again. Let's just copy it, and we'll just repeat it each of these times. It makes for a very long expression, but it does work.

So now it matches 299, but it does not match even 256.256.256.256. That's the upper limit. 255 is as high as our expression goes now. So we learned not only to match IP addresses, but we learned a valuable technique about how to match numbers as strings using the regular expressions. Keep this technique in mind, because we're going to be using it in the next couple of movies as we look at matching dates and times.

There are currently no FAQs about Using Regular Expressions.

 
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.

* Estimated file size

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

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.