[No Audio]
Welcome back again in the course. In the
previous tutorial, we have started our
discussion about regexes. We will move ahead
with the topic of regexes in this tutorial also.
The next concepts in regexes is that of
quantifiers. Quantifiers are used to denote
the repetitions. There are three quantifiers
that are commonly used called the question
mark, a plus, and a star. The question mark is
used for indicating zero or one times of
repetition, the plus is used to indicate one
or more times of repetition, and the star is
used to indicate zero or more times of
repetitions. So, let us learn the question
mark first. I will write a simple Regex.
[No Audio]
This Regex means that, we need to have a zero or
one instance of letter a at the beginning,
followed by two mandatory letters of a.
The question mark after the letter a means that
the letter a is kind of optional at the
beginning, that is zero or one times.
Let us define some input text.
[No Audio]
I also include the code for
displaying the possible matches.
Let us cargo run this now.
[No Audio]
It has returned 2 matches,
the first words, word matched because
the first a is a kind of optional, and it is
not present in the word. The second one also
matched, because the first optional a is
present in it. Let us do one more example,
I will define a new Regex and also a new input text.
[No Audio]
In this case now, the Regex means that at the
start, we need to have a mandatory letter of
b followed by an optional a. In the input text,
the first word will not match, because it does
not have the mandatory letter of b.
The second word containing ba will match.
The next word containing only letter b will also
match, because the letter a is option. Lastly,
the last word containing ba will also match.
Let us cargo run this.
[No Audio]
Let us now cover a nice use case
of this quantifier, where you can use
it to determine some files of a particular
type, let me write a Regex.
[No Audio]
This Regex will match any text that contains
the mandatory .rs at the end. The name of the
file should have a word contains 0 letters,
or at most 3 letters. Let us write some
input text to test the Regex.
[No Audio]
In this case, it will match the first two
file names, but the third one since the length
of the fourth file name is greater than three
characters, will not match.
Let us cargo run this to confirm.
[No Audio]
The next quantifier is that
of the plus, which is used to indicate one or
more times. Let us comment out the
previous code and start fresh.
[No Audio]
I will define a simple Regex.
[No Audio]
This Regex means that we need to have
one or more times of letter a in the input
text for a match to occur. Let us add some input text.
[No Audio]
I will also add the code for
displaying the matches. This will now match
the first word containing a single a, the
second word containing double a, and the third
word containing aaa. However for the
word baab, it will only match the couple of
a's, and from the last word, it will only match
a single a. Let us execute to confirm.
[No Audio]
To find a file which may contain any number
of characters, but has a .gif extension, we will
use a Regex which will look something like this.
[No Audio]
This Regex will match anything that
starts with any number of characters from one
onwards, but that ends on the letters of
.gif. The slash before the dot is used to
make sure that the dot is used in its literal
meaning and not for making any type of
character. Let me add some text to test this Regex.
[No Audio]
Let us execute the code now to see the results.
[No Audio]
The next quantifier is that of the
star, which is used to indicate zero or more
times of repetitions. For instance I will
write a simple Regex, which will match any
text that starts with a mandatory letter of
a followed by zero or more times of letter b.
[No Audio]
Let us add a sample text to test.
[No Audio]
In this case, it will match all the three
words in the text, let us cargo run this to confirm.
[No Audio]
Let us now move to the next topic, but before
that, I will comment out the previous code
and start fresh again.
[No Audio]
Sometimes, we may do not
want to have unlimited number of repetitions,
but rather we are interested in some limited
repetitions. To mention the limited
repetitions, we will mention the least and
most number of times of repetitions inside
the curly brackets, let us use it inside the Regex.
[No Audio]
This Regex will match any text
containing between three to five number of
characters, let us add a simple text.
[No Audio]
Let us also add the code for displaying the matches.
Let us cargo run this now.
[No Audio]
You may note that, it only matches the words
that are between the given range of characters.
For those words who are greater than length,
only their first five characters have
matched. You may use the word boundary meta
corrector that is slash be at the start enter
at the end of the Regex to enforce to only
matches the words, and do not return partial
word from the text. Let me add and cargo run again.
[No Audio]
You may note that, the partial words are not
being returned now. A nice use case of
limited repetition is, to limit the number of
digits in the fraction and whole number part
of a number. For instance, let me add a Regex.
[No Audio]
This Regex will make sure that the number
of digits in the whole and fraction parts are
between one to three digits with a dot
between the two parts. Let me add some text for testing.
[No Audio]
Let us cargo run this now.
[No Audio]
Again to exclude the last
number and make sure that, only that
it only returns the valid numbers, we will use
the word boundary at the beginning
and at the end of the Regex.
[No Audio]
Now the last number is
being excluded. Sometimes we may know the
least number of times, but we may do not know
the maximum number of times the repetitions
will occur. In that case, we will only mention
the first number in the curly brackets and
we'll keep the second number empty, let me add a Regex.
[No Audio]
This will match any text containing three or
more digits. Let us also add some text for testing.
[No Audio]
In this case, it will only match the first
three numbers, since they contain three or
more digit, the last number will not be
matched. Lastly, we can mention a fixed
number of times of repetitions by placing a
single number within the curly brackets, this
is quite simple and will be covered in the
next example. Let us now move to the next
topic, which is capturing groups, I will
comment out the code first.
[No Audio]
Sometimes the pattern we want to define may
be a bit more complex, and therefore we may
want to break it down into its individual
components for the sake of simplification.
This can be done by placing parts of a
regular expression inside parentheses to
create groups or parts within a regular
expression. Consider a case for detecting the
dates which are given in the form of year,
month, and day. The year is in the form of
four digits, followed by a month containing
two digits, and lastly the day containing two
digits. We will write a Regex for each part
of the year, day, and month separately using
the capturing groups. The Regex is going to
be simple, so let us write it and then we will explain.
[No Audio]
The Regex in the first smooth brackets will
match the year. The year contains exactly four
digits, so inside the curly brackets we wrote
four, which will match exactly four digits.
Next we have a mandatory dash, this is
followed by another group which will contain
a couple of digits for the months, followed by
another mandatory dash, and finally another
group for the day containing two digits. Let
us write some dates in the input text now.
[No Audio]
Lastly, we will add the code for displaying
the result. The value at the index zero of
the capture will contain the full match, and
the individual groups will be in the
remaining of the indexes. In particular, the
value at the index 1 will contain the year
group, the index 2 will contain the value
for the month, and the index 3 will contain
the value for the day. Let us add a suitable
print statement to display the result.
[No Audio]
Let us go go run this now.
[No Audio]
With this we end
this tutorial. There are many details of Regexes,
but we intentionally leave them for now
for the sake of briefness. Since this is a
Rust course and not a regular expression
course, therefore we will limit it to these
details. For those of you who are interested
in learning more about Regexes, they may take
our other course on regular expression, you
can ask for a coupon of that and I will give
you away a free coupon for the course. For now
the details covered in these tutorials are
enough to give you a quick start and build
simple regular expressions. See you again and
until next tutorial, enjoy Rust programming.
[No Audio]