Every IT professional will encounter Regex at some point in their career. Contrary to how it looks, Regex is not just some funny strings of random characters. You can in fact find it powering some of the world’s most critical technology infrastructure. Regex is supported out of the box in many different technologies, so learning just the basics can really accelerate your automation workflow. This post will teach you the basics and give you some real-world applications of Regex for Network Engineers.
Well, this is Regex:
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
Yeah, I know, looks kind of like hieroglyphics, right? Well, this may look like a foreign language, especially if you are just starting out, but as with any kind of new language, we need to work through the basics first.
Regex, or regular expression, is a pattern matching engine used to find or parse text and outputs for specified patterns. Regex is built into tools like Vim, grep, and even Python! We can use Regex to parse text and outputs so we can get only the data we need.
Well, there are patterns all around us in the networking world. A MAC address is nothing more than a pattern of some characters that can be A-F
and 0-9
followed by a period or colon (depending on who you ask). And take the IPv4 address for example. If you had to teach someone how to spot an IPv4 address in a random list of characters, what would you tell them?
Well, you might say an IPv4 address is:
Is this starting to make sense?
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
Well, hold your horses, we’re almost there. Let’s quickly go over some basics.
Many people, myself included, use the website Regex101 when making and testing new patterns. With this we can actually see what the Regex engine is thinking when it matches our patterns.
You can see that since I typed “text” in the Regular Expression
box, it will highlight only the words that match “text”. Regex101 also includes a handy Explanation
and other information on the right.
Let’s go into the different syntax you’ll be using to create Regex patterns.
First things first, we need to learn about character classes. A character class in Regex is represented by brackets []
, and it is where you put all the characters like 0-9
or A-Z
that you want to match.
A quantifier in Regex is represented by curly braces {}
, which are used to express how many times you want to match your characters. For example, if you want something to match one to three times, you use {1,3}
. The comma ,
acts as a “through” in Regex.
As you may have noticed, Regex uses a lot of regular everyday characters like .[]{}/
as their pattern syntax. But what if you want to match literally something like the period .
? Well, any characters like that can be matched by adding an escape character before the character. In Regex this is represented by the backslash \
.
So .
becomes \.
to match. And, yes, you can even escape character the escape character by doing a double backslash \\
.
So that is a very basic overview of Regex. So enough talking—let’s get to work! Let’s look at our IPv4 instructions again:
For digits
we’re going to make a capture group for all possible digits. This would be written as [0-9]
.
For one to three
we’re going to use our handy quantifier to express that range. This would be written as {1,3}
.
Finally, the period .
is a special Regex syntax, so we’ll need to use the escape character \
. This would be written as \.
.
Okay, now let’s just repeat that—don’t forget to omit the last period .
!
Look at that!
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
Not so scary now, is it? So let’s put this new pattern to work. The following is a simple show ip route
that, of course, contains IPv4 addresses.
Let’s drop our pattern and see what happens.
SUCCESS!! Regex with our pattern can now match any IPv4 address we throw at it!
Thanks for reading and spending some time learning Regex with me. This, of course, is a very basic overview that just scratches the surface. We can continue to improve our pattern to make it more and more accurate at matching IPv4 addresses. For example, 999.999.999.999
would be a valid match with our pattern, but not a valid IPv4 address. This article is just meant to be a good jumping off point for you to make your own patterns.
And I hope you do feel empowered to go out and make your own Regex patterns! There are so many novel and unique ways Network Engineers are using Regex today. One of the most popular use case is parsing the CLI outputs from our network devices. There are many open sourced projects that need custom Regex patterns for the many different network devices out in the wild. We’ll go through some of those and how you can contribute to projects like NTC Templates and Cisco’s Genie Parsers in a later post.
In the meantime, can you work out a pattern to match MAC addresses? Try it out, and let me know your solution in the comments below!
Share details about yourself & someone from our team will reach out to you ASAP!