- [Narrator] In this self check exercise,
we'd like you to take the string 10 plus five
that represents an addition problem,
and use a regular expression
to break that string into three pieces of information
representing the two operands
and the plus operator in between,
and then to display those pieces of information as well.
So go ahead and pause the video,
give it a shot,
and then come back to see the answer.
Okay, first let's go ahead and import the re module,
as we always need to do here,
and this is the regular expression search
that we're performing.
You can see we have a raw string literal
in which we're looking for a subexpression
that we're going to capture,
which consists of one or more digit characters.
Then we have a space
and another sub string that we're going to capture,
and in this case we created a custom character class,
looking for the arithmetic operator characters
minus, plus, times, and divide.
And then we have another space.
And then finally we have another digit,
one or more digits,
that we would like to capture.
So we have three subexpressions that we want to capture,
and we're going to be trying to match
those inside of the string 10 plus five.
Now of course,
we specifically wrote this out
to match up with that string on purpose here,
so let's go ahead an execute that.
Now, let me expand these others snippets here.
So if you want to see all of the matches,
remember you can call the groups method to do that,
so you can see we matched three substrings,
and of course,
once we have those substrings,
we could do things like convert these
into actual numeric values
and use them in a real arithmetic operation
if we wanted to.
Down below here,
we want to just also show you
that you can individually access each of those,
so let's just go ahead and do that,
and you can see again,
starting from index number one,
not index zero,
you can get the values that were captured
by the three subexpressions
in the original, regular expression.