Hello, everyone.
Today, we'll talk about regular expression, what is the regular expression?
Regular expression is a specific pattern that provides concise and flexible means to match streams of
text, such as particular characters, words or patterns of characters.
This is a definition from Wikipedia.
So in short, regular expression is a way to match streams.
So it is very useful in text editing and the text searching.
Blackhill expression is not just supported by C++, it is supported by almost all languages PO, Python,
Kitco, Java, you name it.
So first I'll talk about the regular expression grammar in general, which is the knowledge that is
transferable to other language as long as the language of expression is supported.
And in later video, I'll talk about the specific features that C++ 11 Standard Library provides.
If you are already familiar with the regular expression and you just want to see how it is used in C++
11, you can skip the rest of this video and jump to the later ones.
So let's first look at our simple example.
This is the infinite loop and in the loop at first read a stream from that standard input and then create
a lively expression and then check if the string and the regular expression matches and then print out
the result.
This is a very simple example of regular expression, it can only match a string of ABC and nothing
else.
So if we run the program.
And that's and a stream ABC, and it matched if kept to ABC, it's non-match A, B, C, C, it's not
a match.
So the only thing can match is ABC, lowercase, ABC.
Now, suppose I don't care about the case, I want to match ABC, whether it's lower case or uppercase,
to do that, I can add a flag.
It's called I case.
You can call case.
And let's remove the extra calm and then run the program.
Now, if I earned capital, ABC, it match, ABC match, ABC match, so the matching is ignoring the
case.
Now, this is very simple string match, the power of regular expression is in the special pattern characters.
A special pattern character has a special meaning when it appears in the regular expression.
The first one I'd like to introduce to you is a thought.
A thought means any character except in life.
So if I had thought after ABC this will match any stream that start with ABC and followed by one character
and that character can be anything except the new and I know that's on the program.
Oh, hey, if we type A, B, C, D, matched A, B, C, E and matched, but if we type A, A, B,
C, D, it's not a match because we have an extra A at the very front.
So that means any character except a new light that's coming out this one and try another regular expression.
This one has a question mark int it.
What does the question mark me.
A question mark means zero or one preceding character.
So if we learn the program.
A, B, C, that's a match, a, b, c, c, that's not a match a, B is also a match.
So questionmark means zero or one preceding character, in this case, the preceding character is sea.
So this will match a B followed by either zero or one C.
Now, let's come on to this one and chai.
Heigel expression that has starring it what does storming a star means zero or more preceding characters,
so not just one, it can be any number of preceding characters and that's one that.
So now A, B, C, C can match a book you see is also match A, B is still a match and A, B, C is
not a match because we have an extra A in front.
Now, let's try another one.
This microexpression is A, B, C plus, plus means one or more preceding character, so it cannot be
zero.
It has to be one or more on on it.
How ABC is a match, ABC is not a match.
So the only difference between Price and the star is if there's zero C, then it's not a match for the
price.
And let's try another one.
This one has a, B, and then square brackets C, D, and then stop.
We know that star means zero or more preceding character.
So what is the preceding character in this case?
The square brackets.
Means any character inside of the square brackets can be matched.
So in this case, either C or D can be matched.
The preceding character can be either C or D.
Now let's run the program.
So if we do a, B, C, it's a match a, B, it's still a match because star means zero or more A,
B, C, D, it's also match A, B, C, C, C, D, D, also match.
And if we do A, B, C, C, that's not a match because we have an extra B over here.
Now, let's try another one.
This is the opposite of the previous one.
It means any character that is not inside of the square bracket is a match.
So if we run this program.
And A, B, C, maybe D, it's not a match, A, B, C, not a match.
A, B, B, it's match a, b, b, b, b, b, so w whatever.
So anything that is not E, not C or D can be matched a B there's also a match.
Now, let's try something different.
So now we have three over here, the three means exact three appearance of the preceding character.
So ABC to be deemed not a match, a B DTD, that is a match because we have three days after A, B,
A, B, C, C, C, also average A, B, C, E, C, match A, B, C, D, not a match.
No, I have four A, B, C, D also not match.
I only got two.
Now, suppose I do three karma, this means three or more preceding character.
So if I do A, B, C, the it's not a match because it's not it doesn't have to be A, B, C, D, D,
d, d, it's a match a, b, c, d, d, d, d, d, d, d, it's a match a, b, c, c.
It's also a match.
And if I do three come five, that means either three or four or five preceding characters can be matched.
So to run again it's A, B, C, C, D at to match.
That's five.
No, that's true.
ABC, CTT, this is a five ABC, DTT, not a match because I have six.
Now, let's do something else.
Now, what is this, this means all so either this one or this one can be much.
So let's run the program.
Now, ABC is still a match.
The air is also match, the G is a match to a B.
That's not a match.
A, B, C, D, E, F, that's not match o, k.
As you see, we use square brackets to define a category of characters that can be matched by what if
we want to match the square bracket itself?
To do that, we can use the escape sign and followed by the square bracket.
No, not this one.
So this is a match of either ABC or Dí followed by F or G or a square bracket.
Now, let's do something else.
This one has parentheses in it, parentheses are used to define a group, and what's be matched by this
group is called a sup match.
The backslash one means the match will appear at this position again, and that the backslash before
the backslash is to escape the backslash.
So now let's run the program.
So ABC that matched the group and the U.S. and then ABC.
So this is a match.
So this is the group first group and then this is the D.C. match over here.
And this is the backsplash one.
And we could also have more than one groups, and in this case, we have Group one A, B, and then
group to the plus and then group to appear first and then group one on the program.
So now, in order to match, I have to have Group one, A, B, and then C, G, C, and then group
to a B again.
Another group two is the key and then group A, B.
So this is a match.
But if I have less likely, this is not a match.
And lastly, let's do something meaningful, let's search an email address that and ways dot com.
And we can do that with this.
So this means what character?
This is a pre-defined character class which defines a group of characters.
And in this example, W. means what character it includes digit number or underscore.
So any one of them can be matched with this expression.
And what character Prast means one or more, what characters and come this star is escaped with.
So it is not the same as this.
They start so they start with the wrong character.
Not all that moment.
Savulescu and Kim Dotcom that some Bill Q at Yahoo about how much you kill at sea.
All right, that's not a match.
There are a bunch of other predefined character classes and here's a table for them.
This is from C++ dot com and we've already seen W means what character and deed means, decimal digit
character, uppercase, upir means uppercase and punkt.
This is pretty useful.
It's for punctuation marks Alpha.
As for alphabet characters, so the predefined character classes provide shortcuts for the frequently
used the character classes.
OK, enough of that ruckus like X match.
Now let me show you a Radack's search.
What did the regex search does is instead of matching the regular expression to the entire string,
it only search the stream to see if there's a substring that can be matched with that regular expression.
So if we come out, this one coming out of this one and like, let's try the simplest case, ABC dot
and then run it.
So now if we search A, B, C, D, E, this is still a match.
This is the same as like a match.
Now let's do A, B, C, and this is still a match.
This will not be a match for the regex match, but for Raggett search, since we only care about substring.
So this is to match.
And now let's say I have a B.S. and a bunch of different things.
And this is still a match because we have a substring of ABC that it can be matched.
So this is how the regex search works.
Saying now I want to add more constrained, I want to say only when ABC is at the beginning of the screen,
it can be counted as a match.
If ABC appears to the middle of the street or at the end of the screen, that doesn't count.
To do that, I can do this.
Now, let's run the program.
OK, now I do.
A, B, C, C, D, that's a match, a, b, c, d, that's a match A, A, B, C, D, that's not a
match because ABCDE is not at the beginning of the string.
And if we don't have the character sign in the beginning, then this is a match for regex such.
Now, let's say instead of such a B.S. start in the beginning, I only want to search that in the end
of the stream.
To do that, I only need to add a dollar sign.
And then that's on the program.
Now, if I do A, B, C, D, it's a match because ABCDE is that at the end?
Hello everyone.
Last time I'm giving you a tutorial of how regular expression can be used in C++ 11 and we've covered
that the two APIs like X match and like X search, which are provided by the How to file like X, and
we spend most of the time talking about the like regular expression grammars.
The regular expression grammars are not something that's particular to a C++ leverne.
It is a standard Arikara expression and can be used in any language that is support that supports regular
expression.
However, you need to be careful that there are different flavor of regular expression out there and
make sure you are using what you want to use, C++ 11 provides six types of regular expression grammars,
and by default, it is always using that ECMAScript regular expression grammar.
ECMAScript is by far the most powerful, regular expression grammar.
That is why it is that default.
But you can always choose one of the other grammars.
Basic extended.
OK, grab an E grab that's like an example and say we want to find a stream that matches Carrott.
ABC thought plus and we know the PLAST means one or more preceding character.
So this will match a stream that start with ABC and then followed by one or more any character.
So if we run the program.
So ABC, that's not a match, ABC is a match ABC to a bunch of different characters.
It's still a match.
Now, let's say I don't want to use the ACMA regular expression grandma.
I want to use the crabs regular expression grandma, because I've been using crab all the time and I'm
very familiar with that.
To do that I need to do is add a second parameter to the regular expression constructor.
It's called like X constants grab and now let's run the program.
So, again, we do A, B, C, that's not a match, a, b, c, d, e, that's also not a match.
So A, B, C, D pass.
That is a match.
So when we're using a grabs regular expression, grandma, the plus is not a special key.
It doesn't have the meaning of one or more preceding character.
It's just one plus regular character.
That is why we can do A, B, C, followed by any character and then followed by the process.
This is a match.
So this is how the regular expression grandma can be switched to a different Gramma's.
The next thing I want to talk about is something much this is the example from last time, it's a regular
expression to match all the email addresses that end with dot com.
Now, suppose I not only want to find all the email address that at that ends with dot com.
I also want to extract a portion of that email address.
I want to extract all the user names.
I want to extract all the domain names.
How do I do that?
The first thing I need to do is define a group, I define a group for the things that match the user
name, and then I define a second group to match, to contain all the things that match the domain name.
And then the second thing I need to do is create an object of as much as match is a template cost match
results of.
And what it does is, just as the name indicates, to store the results of the match, the detailed
results of the match.
Then I call the function like a search string m e what it does is it search inside the string with the
regular expression E and see the results in M..
Now we have all the results stored.
The next thing is to fetch all the results.
And we do that by calling a bunch of member function of S match.
And here's how to do it.
The function M size returns the number of matched results that's stored in M, and then I iterate through
each of the match results and print them out and at the end I print out that prefix and suffix.