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
Grouping characters allow you to apply modifications to multiple characters the way you have learned to do with single characters.
Google URLs
google.com
google.net
google.org
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:
img_01.jpg
img_02.png
img_03.gif
img_04.png
img_05.gif
img_06.jpg
Exclude:
mov_01.avi
2 )
Match:
projector
protractor
proctor
Exclude:
prospector
3 )
Match:
img_sm_01.jpg
img_01.jpg
img_sm_02.jpg
img_02.jpg
img_sm_03.jpg
img_03.jpg
img_sm_04.png
img_04.png
Exclude:
img.gif
4 )
Match:
www.github.com
github.com
www.teamtreehouse.com
teamtreehouse.com
api.github.com
Exclude:
jsfiddle.net
www.jsfiddle.net
Most of the regex wildcards I've shown
you are used to match single characters.
0:00
But what if you want to
match groups of characters?
0:05
You can use parentheses to group
parts of an expression together.
0:08
Let's look at how we can use
grouping to avoid the repetition
0:13
in the regular expression
from the last video.
0:17
If I put the three alternate
expressions into a group,.
0:21
Like so, we can separate that part
from the part of the expression that
0:32
matches the same characters space boat.
0:37
Now the grouped part of the expression
can be thought of as one unit.
0:41
It matches one of the three possible
substrings, toy, sail, or tug.
0:45
The test string still has to end with
space boat to be matched though.
0:52
Here's a similar challenge,
write an expression to match
0:57
google.com and google.net,
but not google.org.
1:03
You can copy those three test
strings from the teacher's notes.
1:08
Pause the video and give it a try,
and then I'll show you how I did it.
1:16
Each string begins the same way,
namely, google dot.
1:22
I'll start by typing that.
1:28
Then I can group com pipe
net within parentheses.
1:30
That will match only the top two strings.
1:40
One thing to note though.
1:43
While this expression works for
1:44
these strings,
that dot could get us into trouble.
1:45
Remember, it's a wildcard character, so
it would match the dollar sign also.
1:49
If we want to specify that
it only matches a dot,
1:56
we can add a backslash before the dot.
2:00
Now this backslash dot is considered by
the parser to be a literal dot character.
2:07
You may have also wrapped the dot in
square braces, which would also work.
2:13
Using a backslash is a little
more concise though, so
2:18
I wanted to make sure
that you knew about it.
2:22
Grouping lets us treat more
complex patterns, like letters.
2:25
In other words, we can make them
optional with a question mark or
2:30
allow repetitions with a plus or
star, for example.
2:35
Let's add the string www.google.com.
2:39
To include this entire
string in our match,
2:45
we could type www slash dot.
2:50
But putting a question mark after
the dot will only make the dot optional.
2:54
In other words,
wwwgoogle.com with no dot is a match.
3:00
We want everything before it to be
optional as well, not just the dot.
3:08
If I group this pattern,
the whole group becomes optional.
3:15
Try a few of the exercises
in the teacher's notes for
3:21
more practice with groups.
3:24
You need to sign up for Treehouse in order to download course files.
Sign up