[No Audio]
A Regular Expression is a search pattern that
describes a certain text that we want to find
in the input text. This means that, we will
have two inputs to the system, there is the
input text and the regex, and the output of
the system will be the instances of the
search pattern that the system is able to
find in the input text. Please note that, this
is going to be very precise and short
tutorials on regexes or regular expressions.
And if you want to learn more in depth about
regexes, then you may reach out to me, and I
will give you a free coupon for one of my
other courses on regular expressions for
having an in depth tutorial on regexes. Since
this course is about Rust, so therefore these
tutorials will not be very much elaborative,
and will be therefore short and more precise.
The search pattern may be used for a text
searching to perform operations such as
find and replace, finding some particular piece
of information in the text, such as email
addresses or phone numbers. Due to their high
importance in problem solving encountered in
programming tasks, the regexes in many
programming languages are implemented as part
of the standard library. Rust does not
provide it in the standard library, since the
intention in Rust is to keep the standard
library as shrinked as possible. However,
there are some useful crates which can be
used in this regards. In this and some of the
upcoming tutorials, we will be looking at the
crate called regex, which is typically used
for regular expression implementation in
Rust. I will first add it to the terminal file.
[No Audio]
Let us first bring the relevant
modules in crate into scope.
[No Audio]
Let us create a simple regex using the new function.
[No Audio]
The new function returns a result enum,
so therefore, we have unwrapped it to
obtain the regex. If the regex is not
syntactically correct, then it will return an error.
The arm before the regex is used to
indicate to Rust that it is going to be a raw
string. The text inside the double quotes is
our regular expression. There are many
constructs in a regular expression, and the
most fundamental one is called character
class. Character classes are being indicated
by square brackets. It tells the regex engine
to match only one out of several characters
that are being mentioned inside the square
brackets. In this example, we are telling to
match it in the input text at the start, one
of the three letters of either letter b, or
letter r, or letter t, followed by a mandatory
letters of ain which must be matched. Now,
let us define a string which we want to
investigate for possible matches. I will
define a reference string containing some text.
[No Audio]
Next, to determine if the regular expression
has a match in the input text or not, there
is a function called is_match,
which returns either true or false. Let us
use it inside the print statement.
[No Audio]
Let us cargo run this now.
[No Audio]
There is a match. To know where exactly is the
first match in the input string, the crate
provides another function called find which
returns the starting and ending index of the
first match in the input string. Let us call
it inside the print statement and display the result.
[No Audio]
These functions are useful, however, they do
not provide the specific text which has been
found as a capture. The crate provides a
convenient iterators for matching an expression
repeatedly against a search string to find
successive non overlapping matches.
Let us use it using a for loop.
[No Audio]
Let us print the individual captures,
inside a print statement now.
[No Audio]
The capture_iter returns a capture
structure which has the individual captures
inside each store, which we can access using
the index of zero. There are some regexes
which has different paths to match, and the
indexes are used in those cases to refer to
the different parts of a single regex
which has been matched. We will look into the
details of that later on. For now, it is
sufficient to know that the individual match
texts are being accessed using this syntax.
The result in this case would be rain and
pain. Let us execute and then we will explain.
[No Audio]
The first two letters of r in the words
does not match, because after the are the
rejects demands the letters of ain, which are
absent after the first two letters of r.
The last are matched, because we have the letters
of ain afterwards. The first letter in the
second word does not match to any of the
characters in our character class, and is
therefore not part of the match. The letter p
followed by the letter s matched, because the
letter p is in the character group, and is
being followed by the mandatory letters of ain.
The dot in regex will match a single
character including letters and digits, dot
should not be used inside a character class.
Let us modify the regex and put a dot after
the character class. This means now that in
order to have a match, we need to have either
one of letters p, letter r, or letter t at the
start followed by any character and then a
mandatory letters of a, i, and n, let us execute.
[No Audio]
You may note that, it also makes the extra are
in the rain due to dot. The corrector
class is quite useful, and can be used for
spell checking. Let me add a new regular
expression for checking the spelling of gray.
[No Audio]
The spelling will be correct, if we have
letters of g and r followed by letters of
either a or e, followed by a mandatory letter
of y. Let us add some text for checking.
[No Audio]
Let us paste the code for the possible
matches now and cargo run.
[No Audio]
You may note that only the
valid part of the spellings are
being returned. Similar to the corrector
classes are correct arranges which are used
to check if characters in a certain range are
part of the text or not. For instance, to
check for all the lowercase characters, we
will use the syntax which will look like this.
[No Audio]
The small a followed by a dash and then z
means, all the characters from A to Z.
The overall meaning of the regex is now we need
to have a small letter character at the start,
followed by the mandatory letters of a, i, and n.
That is used some example text
[No Audio]
Finally, I will add the code for printing
out the relevant matches.
[No Audio]
Let us cargo run this now.
[No Audio]
It returns the possible matches. The last
word did not match, because at the start of
the word we do not have a small character
letter. We can mention multiple ranges inside
the square brackets also, for instance, I will
include all the upper letters by mentioning
capital A-Z. This will now mean that, at
the start we need to have at least one upper
or lowercase character. Inside the square
brackets, we can exclude certain ranges also.
The exclusion is done with the help of the
cap symbol at the start of the range.
For instance, if I want to exclude all the
lowercase characters from the start of the
match, I will mention something like this.
[No Audio]
Let us execute to see what
happens to the matches.
[No Audio]
You may note that, it has just returned a
match for the word of 0ain. And this is
because, all the remaining words has a
small letter character at the beginning and are
therefore not being matched. There are some
shorthands for the frequently used character
classes, and they include a slash small w,
and slash small d. Small w is a shorthand for
character class including all upper, lowercase
characters, digits, and underscore. On the
other hand, \d is shorthand for all
digits from 0 up to 9, and let us have
some examples. I want to make a local
telephone number which is six digits long in
our are, so I will write a regex which
contains only six digits.
[No Audio]
Next, I will define a simple text containing
some text and one telephone number.
[No Audio]
Let us also include the code for printing the matching.
This will now return the match for
the phone number from the input text.
Let us cargo run this.
[No Audio]
Okay let us take a short quiz. I will write
simple regex and some input text.
[No Audio]
Can you guess what this regex will return,
I will give you five seconds to think.
[No Audio]
In this case, the regex will return the two
phone numbers. This is because the regex
means that, at the start we need to have a
digit followed by any five characters. A cap
before the shorthand character will mean a negation.
For instance, if we use, if we use
cap followed by \d in this regex, then it
would mean to match anything which is not a
digit. The small \s is another very
useful shorthand that is used for making a
space character. Next, let us talk about the
starting and ending anchors. The cap when
used outside the square brackets is used to
indicate the start of the input text.
For instance, let me write a regex.
[No Audio]
The cap when used inside
square brackets or when used in
connection with shorthands, has a meaning of
navigation, but in this case, it will match
at the start of the input text before any
character in the input text.
So, let us add some input text.
[No Audio]
I will next include the code for printing the matches.
[No Audio]
In this case, the cap will match at
the start of the input tag, and then there are
regex demands the letters of a followed by
b and then a. Since the input text has this,
therefore it will match. Let us cargo run to confirm.
[No Audio]
if I remove the starting a in the text, then
it will not match, because the cap matches at
the start of the text, and then the regex
demands the letters of a which is not there,
let us cargo run again.
In summary, the cap is
used if you want a regex to match at the
start of the input text only. Similar to the
starting anchor, we have the ending anchor
which will match at the end of the input text
Let us define a regex and some input text.
[No Audio]
Let me also include the code for matching.
In this case now, the regex will first match
dollar with the ending of the text, and then
from the ending of the text, the last element
should be c, and then the second last element
should be b. Since the input text contains
such a match, therefore the match will be
successful. A key point to note is that, the
starting and ending anchors do not match any
characters themselves, they are just telling
the regex engine to match at the start and at
the end of the input text.
We can use the starting and
ending anchors at the same time,
let us define a regex.
[No Audio]
In this case, now the cap will enforce to
match at the start followed by bc, and the
ending dollar will enforce to match from the
end. The dollar sign requires that last
letter should be c, and second last letter
should be b. Since all these requirements are
being fulfilled, therefore there will be a
successful match. Let us take another quiz, I
will add a regex, and you will have to guess
what it will match.
[No Audio]
I will give you five seconds again.
[No Audio]
This will match any input text which contains
exactly two digits. Let us now talk about
word boundaries, which is done with the help
of a special metacharacter called \b.
It will match at the three places, that is before
the start in the input text before any input
character, or at the end of the input text
after there is no character, and between two
characters, if the first one is a word
character and the second one is not a word
character. Again I will repeat that, it will
match either at the start or at the end, and
between two characters, if one is a word
character and the second one is not a word
character. Let us use it inside some
examples. I will include a simple regex.
[No Audio]
let me also include a simple text.
[No Audio]
Now I will include the code for matching.
In this case now, the \b will match at the start
before any character. Like cap and the dollar,
it does not match any character, but it rather
matches a position which is the start and end,
and any other position where one of the
position after or before is a word character,
and one position after or before is an non word
character. In this case, the \b will
first match at the start position. After the
start of the regex requires any word
corrector, so it will match the letter edge
in the word of Hi. Next, the position between
the two words of Hi and my, it will match the
position after the letter i, because before
this position, there is a word corrector, and
after this position there is a space, that is
non word corrector. However, the pattern will
not match, because after this position, we do
not have any word corrector, rather we have a
space. Next, it will match the position just
before the letter m, because it also satisfies
the requirements. After the position of m,
there is a word corrector of m, so it will
return a match there also. In summary, it
will return all the starting letters of the
words. Let us cargo run to confirm.
[No Audio]
A star in the regex means zero or more
times of a certain pattern. So, if I put a
star on the \w, it will mean zero or more
times have any character. In this case now, it
will match the complete words in the input
text. Let us cargo run this to confirm
[No Audio]
We end this tutorial here
as it is getting a bit lengthy.
In the next tutorial, we will try to wind up
the topic, and we'll cover some more stuff
related to regexes. Do come back for covering
that, and until then happy Rust programming
[No Audio]