Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn how to write simple regular expressions with optional characters.
Practice
Copy each set of test strings into regexpal. Using what you've learned so far, create a regular expression that will match all of the strings in the set.
1 )
ladybug
ladybugs
lady bugs
2 )
ladybug
lady bugs
lazy bug
lazy lug
3 )
ladybug
lazy lug
lazy slug
hazy slug
4 )
ladybug
fading rug!
-
0:00
If I ask you whether these two words match,
-
0:03
you'd most likely glance at them both and say, hm, yes.
-
0:08
However, a regular expression doesn't work that way.
-
0:11
Instead, it uses a parser which compares each character in a regular
-
0:16
expression with a character in the string in the same position.
-
0:20
In other words, the regex parser requires two things.
-
0:23
An expression, also called a pattern, and a string to match.
-
0:28
The parser takes that pattern and walks through the string,
-
0:31
character by character.
-
0:33
First, it checks if there is a t.
-
0:36
Then if the t is followed by an o.
-
0:39
Which is followed by a y, then a b, o,
-
0:43
a, all the way to the t at the end.
-
0:47
At that point, the parser knows the string is a match.
-
0:51
I'm going to type toyboat into regexpal, in the regex pane, in the testing pane.
-
0:58
You can see there is a match selected here.
-
1:01
This is the basic matching we saw earlier.
-
1:04
Let's say we want to match the singular or the plural.
-
1:09
If I add an s to the regex, we lose our first match.
-
1:14
However, we can make this final s optional.
-
1:18
Match it if it exists but don't require it.
-
1:21
We can do that with another special regex character, the question mark.
-
1:25
The question mark matches the character that appears directly before it
-
1:29
zero times or one time.
-
1:32
In this case, the lower case s is before the question mark, so s is optional.
-
1:38
But if it does appear, it can only appear once.
-
1:41
So this regex will match toyboat or toyboats but not toyboatss with two S's.
-
1:48
Let's use the question mark in the middle of the string.
-
1:50
Let's match toybots, too.
-
1:53
We can put a question mark after the a to make it optional also.
-
1:57
It works.
-
2:00
I'll replace toybots with toyboats again, but this time with a space.
-
2:05
Just like before,
-
2:06
we can handle this with a question mark, making the space optional.
-
2:14
Now let's say that we want to match toy, even if it's capitalized.
-
2:20
Regular expressions are case sensitive by default, so
-
2:23
we'll need to specify that the T can be upper or lower case.
-
2:28
We can tell the interpreter to accept a capital or
-
2:31
lower case t by wrapping both characters in square brackets.
-
2:36
Let me show you what I mean.
-
2:38
I'll put a capital T next to the lowercase t, and surround both with square brackets.
-
2:46
I just added what is called a character set to the regex.
-
2:50
When the interpreter sees it,
-
2:51
exactly one of the characters in the set will match one position in the string.
-
2:57
We can specify as many characters as we want between these brackets.
-
3:01
Let's say we also want to match joy boats with a lowercase j, for example.
-
3:06
We can just put a j inside the square brackets.
-
3:10
There's two things to note.
-
3:11
No matter how many characters we put in the set,
-
3:14
the set will always match only one position of the string.
-
3:18
In this case, it's the first position.
-
3:21
It also makes no difference how you order the character set.
-
3:24
Now I wanted to give you a challenge.
-
3:26
I'll type out two more test cases for you.
-
3:29
See if you can modify the regex to match those
-
3:32
along with the strings it already matches.
-
3:35
I'll add a hyphenated version.
-
3:44
And another version with the B capitalized.
-
3:48
Go ahead and pause the video and see if you can do it.
-
3:52
For the hyphenated string, I replaced the space and
-
3:55
the regex with a character set containing a space and a hyphen.
-
4:02
Note that the question mark now indicates that the character set is optional.
-
4:08
In other words, the character between the y and the b can either be a space,
-
4:13
a hyphen, or nothing at all.
-
4:16
For the second string,
-
4:17
I used a character set to match either a lower or upper case B.
-
4:24
I've added some additional strings for
-
4:26
you to practice on in the teachers notes below.
-
4:28
Take some time to try then now if you'd like.
-
4:32
In the next video, I'll show you how to add a little more power to character sets.
You need to sign up for Treehouse in order to download course files.
Sign up