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 a Regex tester like regexpal or 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 JavaScript.
0:00
It tells the parser to either
match one pattern or another.
0:04
Instead of two pipe characters,
though, it's just one.
0:09
I've cleared the regex
from the last video.
0:12
I'll replace the email portion of
this string, so I just have toy.
0:15
Then I'll add sale, steam,
and tug underneath.
0:21
Now if I only want to match toy or sale,
0:26
I can type them both separated
by a pipe character.
0:29
Notice how steam and tug are not matched.
0:35
This can help you get really specific
about strings that may not follow
0:38
a common pattern.
0:42
To match tug, I could just add
another pipe and follow it with tug.
0:44
What if we only want to match part
of a string with alternation though?
0:48
For example, if I put a space
boat on the end of each of these
0:53
strings, how can I match
each whole string?
0:58
If I put a space at the end of the regex
and boat, only tug is matched.
1:04
That's because the pipe character
naturally groups the expressions that
1:11
separates.
1:15
In other words, I need to put
a space boat after toy and sail too.
1:16
But there is a better way which
I'll show you in the next video.
1:23
You need to sign up for Treehouse in order to download course files.
Sign up