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