Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
You learned about the concept of NOT in the last video. Regular expressions also support the idea of OR. We'll cover that here.
Practice
Copy both the Match and the Exclude set of test strings from each exercise below into regex101. Using what you've learned so far, create a regular expression that will match all of the strings in the Match set and exclude the ones in the Exclude set.
1 )
Match:
grizzlybear
pandabear
Exclude:
koalabear
bear
2 )
Match:
firestation
firehouse
Exclude:
firewalker
fire
3 )
Match:
farmhouse
bighouse
firehouse
Exclude:
house
4 )
Match:
projector
protractor
Exclude:
proctor
Alternation is like the OR
operator from any programing language.
0:00
It tells the parser to either
match one pattern or another.
0:05
Instead of two pipe characters though,
it's just one.
0:09
I've cleared the regex from the last
video and added some new matches.
0:14
Toy, sail, steam, and tug.
0:19
Now, if I only want to match toy or
sail, I can type them both.
0:22
Toy and sail separated by
the single pipe character.
0:28
Notice how steam and tug are not matched.
0:34
This can help you get really
specific about strings that may
0:39
not follow a common pattern.
0:44
To match tug,
I can just add another pipe and then tug.
0:46
What if we only want to match part
of a string with alternation though.
0:52
For example, if I put boat at
the end of each of these strings.
0:57
How can I match each whole string?
1:05
If I put a space and then boat at the end,
1:09
only tug boat is a match.
1:14
That's because the pipe character
naturally groups the expression it
1:17
separates.
1:22
In other words, I'll need to put
1:22
space boat after toy and sail also.
1:27
But there's a better way.
1:35
I'll show you in the next video.
1:36
You need to sign up for Treehouse in order to download course files.
Sign up