- In this video, you will learn about regular expressions.
So, regular expressions are text patterns
that are used by tools like grep and others.
And why is it useful?
Well that is useful in cases
where you don't know exactly what you are looking for.
Now, you can specify the pattern,
a little bit like wild cards, in a shell environment.
If you are going to use regular expressions,
you should always put your regular expression,
also shortly refer to as "regex," between single quotes.
That is because regular expressions
are using characters that can
also be interpreted by the shell.
And you want to avoid any problems
because of misinterpretation.
That's also one of the big things in regular expressions.
The regular expressions
sometimes looks like shell character
but they are not.
So, don't confuse the regular expressions,
with globbing, which is shell wildcards, and so on,
because they look the same but they are not.
Look at this example for instance,
grep single quote, a star, single quote, on a star.
So the single quotes are identifying
that the first 'a star' is a regular expression.
And the second a star is shell globbing.
Regular expressions are very powerful
and you are going to appreciate them.
But they only work in specific utilities
like grep and vim and awk and sed.
There's a couple more utilities you will find
your way automatically.
What you should to remember
If your utility is about finding text
and about treating text,
then there's a chance
that it works with regular expressions.
I will give you an introduction
to the most useful regular expressions.
For more detailed information
consider reading "Man 7 regex",
where the seven is referring to man section seven,
to get information about the regular expressions.
Now, regular expressions are powerful
but regular expressions also are confusing.
Now, why is that?
Well, that is because regular expressions work
with many tools, but there is also this thing
called extended regular expressions
and extended regular expressions
don't always work.
And a regular expression is either a normal
or an extended regular expression.
And if you use an extended regular expression
without using an extended regular expression option
then it's not going to work.
Starting to feel confused?
I can imagine, but I will clarify once I demonstrate.
You should also notice at some scripting languages
like Pearl for instance,
come with their own regular expressions.
And that makes that if you're going to look
up regular expressions on Google, for instance,
you will find information
that doesn't match your environment
and it won't work.
Also confusing is that there is so much information
about regular expressions
that it is difficult to find the difference
between the things that matter
and the things that don't matter.
Now, I've put together a slight
that contains all of the most common, regular expressions.
And you know what?
I am just going to put the entire slide on
on the screen so that we can talk about these examples
and you can use the slide for your own reference purposes.
All right, let me start with grep, grep Anna or users.
What is that doing?
This is showing all lines that contain the text Anna.
Notice that Anna in this case is a regular expression
in order to be interpreted as a regular expression
if you really want to do it a pure way
put it between single quotes.
No difference but it's good practice
to identify your regular expressions using single quotes.
First leg X I want to introduce is carrot,
which is looking for the pattern at the beginning of a line.
Likewise, we can also use dollar,
which is doing the opposite,
which is looking for the pattern
at the end of the line,
and you can combine them as well.
So carrot, dollar is looking
for lines starting and ending with.
Now another one that is interesting
is the slice B slash B
is for end of word.
Let's get back to the original start.
Here we go.
Look at the second line, which is Annabelle.
I want to do a regular expression
that will work on words only.
So how are we going to do that?
Well, I am going to use grab Anna slash B
slash B is the word marker on users.
And there we can see that it prints all lines
that have Anna as a word,
but not lines that contain it somewhere
in another string.
Now we have other regular expressions
which are basically repeating operators
and the repeating operators are interesting,
but I need to modify my users file a little bit
to show you how it works.
So I am adding BT and bit and bite and bolt and boot
and boot and well, something like this.
That should be good enough.
So let's talk about these repeating operators
to start with I am going to show you grep 'b'
B I star T on users.
Now this is an interesting one
because here we have the star
and the star means zero or more times.
So what do we see?
We see, we get match on BT on bit and on bite.
So we are looking for zero or more occurrences
of the, of the Is.
You can also do that in another way, by using a plus.
A plus if you replace the, the star with a plus
then you are looking for one or more times
but as you can see, that doesn't work.
How come?
Well, that is because this is one
of these famous, extended regular expressions.
And if you are going to use extended regular expressions
with grep,
you need grep minus uppercase E
minus uppercase E will look
for the extended regular expression
and without minus uppercase E
it is not interpreted the right way.
Now we also have question mark
what is question mark
Question mark is zero or one time
also an extended regular expression.
So you want to get a match on, on zero or one ice
Well, this is what we get.
That's not the most convincing example.
Let's do that on B O question mark T
which is giving us BT and bot.
Whereas if we use the B plus B O plus T
we also see boot and boot with, with many O's.
Now we can also use a regular expression
that's looking for an exact match.
So I am using grep on BO
and I want to find lines that have exactly two Os
not more, not less.
How do we do that?
By using slash curly brace,
followed by a two
followed by another slash
followed by another curly brace
followed by a T
and a single quote on users.
So this is giving me an exact match.
Now, if you want to, to understand it's a little bit
hard to understand,
but we're what it really comes down
to is a two between braces
but these braces need to be escaped.
And that's why there's this slice before them
and that makes them a little bit more readable.
But this is how it works.
What happens if you don't escape them?
Well, then you don't get a result and it's just wrong.
Now there are other regular expressions as well.
Like for instance, you are looking for a string
that must be a word grep on slash B
Anna slash B on users
for instance.
And now we can see all occurrences where Anna is a word
it's not really necessary to do the B at the beginning
because it works out well in either way.
You also are going to appreciate the one character.
So if you use grab dot, dot, dot, dot on users
can you predict what is going to happen?
It is just showing all of the lines.
Why?
Well, because it's looking for, for characters
any character
that's probably not what you wanted.
If you are looking for four characters in a word,
for instance,
that's becoming more interesting
then you use slash b behind it.
Then it's considering individual words.
Is that convincing?
Well, maybe not, but if we put a B in the beginning
then we get something that really makes sense.
We are looking for four character words right here
and likewise, you can use carrot and dollar
as the start and end of the lines.
Last regular expression
I want to show you is another extended regular expression.
Grep minus uppercase E, followed by SVM pipe VMX
on slash proc, slash CPU info.
And this is actually a useful regular expression
because proc CPU info is a file that exists in the kernel.
And this file contains CPU information.
Let me show you cat on slash proc slash CPU info.
This is the file.
If you wanna do virtualization
in order to do virtualization, you need either SVM
or VMX to be in the result of proc CPU info.
And this is how you can do an either or regular expression.
Let me just fake it
because here we don't see a result at all.
That's not very convincing.
I'm just going to fake it.
And instead of VMX, I'm looking for VME and there we go
you can see that that is actually giving a match.
Now what if we also include PSE
then you can see that both are giving a match.
This either or regular expression
in fact is a very useful regular expression.
Are you feeling confused?
I can imagine.
Regular expressions are confusing
and even the best make errors
while using regular expression.
My recommendation, practice what we have seen right here
and you will get familiar with them
one time, sooner or later.