All right, let's talk about the regular expressions.
What is a regular expression?
It's a text pattern that is used by text processing utilities
like GREP and VIM and other utilities as well.
It's pretty powerful, but also a bit complex.
There's a couple of things that you need to know about them.
First, always put your regular expression between single quotes
to avoid interpretation issues.
Also, don't confuse regular expressions with globbing.
Globbing is about shell wildcards.
That's about file names.
A regular expression is about what is inside your file name.
Also, realize that they look like globbing.
They're not the same.
Look at this example.
GREP, single quote, A star, single quote on A star.
What is that?
Well, we are looking for the regular expression A star.
In the globbing pattern, A star.
And I will tell you shortly what this regular expression A star means.
It's really important to use the single quotes here to avoid any confusion.
Now, do keep in mind that regular expressions are for use with specific tools only,
like GREP and VIM and AWK.
Also, some less obvious tools like SELinux FContext that you will learn about later.
If you want to learn more about regular expressions,
check out the man page in Section 7 for regular expressions.
There's a lot of information in it.
Don't get confused because regular expressions are confusing.
Now, why is that?
That is because we have different sets of regular expressions,
and that's explained because of history.
Historically, it all started with the basic regular expressions,
and they worked with most tools.
Then at a certain moment in time, some tools decided we need more,
and then the extended regular expressions were born.
Now, if it is an extended regular expression,
you must call it using GREP minus uppercase E.
Otherwise, it won't be interpreted the right way.
And to make it even more confusing,
some scripting languages like Perl come with their own regular expressions.
So what is the challenge?
Well, the challenge is that people read about regular expressions.
They go Google it.
They see a nice pattern.
They overlook the fact that, in fact, it's a Perl regular expression.
They try it on the command line, and it doesn't work.
Yeah, that's because it's wrongly applied.
Now, regular expressions are very complex.
I want to show you a couple of examples.
And here we have the examples.
Let me demonstrate.
To show you regular expressions,
I created a file with the name regaccess.
You can also find it in a course Git repository,
but you can only access it after studying lesson 12,
where we talk about software management.
This is the content of my file.
It doesn't really make sense,
but it does allow you to show what regular expressions are all about.
So let's start with what we call the anchor.
As always, to avoid confusion problems,
you need to put your regular expressions between single quotes.
When I use grab caret L,
then we print lines that start with the letter L.
So the anchor caret is for start with.
There's the opposite, which is dollar,
and dollar is looking for end with.
So I'm going to change my regular expressions,
and I make that Anna dollar,
and that shows lines that start with the text Anna.
We also have B.
B is for end of word.
So I could use grab Leah on RegEx.
There we can see all occurrences of Leah,
but if I make that Leah slash B,
then we are looking for words.
So the end of the word, you can only see the separate word.
So it will find the lines that start with Leah,
but not with Leanna.
Also convenient is the one-character grab.
Now let me show you grab caret dot dollar on RegEx,
and what do we see?
Well, we see that we don't have a match,
and that makes sense because the dot in the regular expression
is like the question mark in file system globbing.
It stands for one character, and when I say caret dot dollar,
then I mean I'm looking for lines
that contain one character only, and there's none of them.
You can fix that by using the star,
which is zero or more times.
Now we see all of them because all of the lines
have zero or more characters in them,
but maybe we want to show that on N dot star X.
My mistake, but maybe we want to show that
using grab N dot star X on RegEx,
and there we can see that we match the pattern
where we have zero or more characters
between the N and the X.
Now let me do that in a different way.
Grab B dot star T on RegEx,
and what do we see here?
Well, BT is the match, and everything else
that has the pattern B followed with a T later on
is a match as well.
That's a zero or one character,
and you might think zero or one, is that so very useful?
Yes, sometimes it is useful,
but sometimes you better want to search for one or more times.
Then you replace the star with a plus
just to see that nothing is happening
as this is an extended regular expression.
An extended regular expression must be addressed
with the uppercase E option.
Another example of an extended regular expression
is a question mark,
which means that the preceding character
needs to occur zero or one times,
so dot question mark should show BT as well as BIT.
As you can see, that works.
Next is a repeating operator.
Let's use a repeating operator in grab B-O-N,
and then the complex thing, a three,
with the curly braces and N-E-N on RegEx,
and there we can see it's giving a match
on the line that has Bonnen with four N's.
Why four?
Well, here we have N number one.
Then we have the repeating operator
that is specifying that it should occur three times,
and then we have another one,
so really we are looking for four N's, isn't it?
In the repeating operator,
there's an additional complicating factor,
and that is that the curly braces need to be escaped,
otherwise it doesn't work,
and that makes a regular expression
that is pretty complex.
You can also use for strings that must be a word,
like grab B-N-A-slash-B on RegEx.
Now, what makes it confusing
is that the slash B in the beginning
is followed immediately by the text,
but here you can see that it is effective,
and the final regular expression I want to show you
is the OR option,
so grab minus E-S-V-M pipe V-M-X on RegEx,
and that is showing that lines
with either S-V-M or V-M-X are showing,
so that's what you need to know about regular expressions.
Fun? I don't think so,
but practice these regular expressions on these slides.
I tried to show you the most convenient regular expressions,
and I really think it's going to be beneficial
if you know how to work with them.