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
It's important to be able to specify that a regular expression starts matching at the beginning of a string and match all the way to the end. Learn more in this video.
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:
tart
Exclude:
start
tartan
2 )
Match:
img_01.jpg
img_02.png
img_03.gif
img_04.png
img_05.gif
img_06.jpg
Exclude:
6_img_07.gif
mov_01.avi
3 )
Match:
projector
protractor
projected
Exclude:
proctor
my projector
projecting
projectorlight
4 )
Match:
3456
34567
Exclude:
23456
345678
Notice this string I typed in
the last video, wwwgoogle.com.
0:00
Even though we've grouped
the www dot together,
0:06
the regex is still finding a partial
match in this test string.
0:09
As you can see, it does this by
starting to match the pattern,
0:14
later in the string,
at the fourth character.
0:18
We can see the opposite problem
by typing another test string,
0:20
with extra characters at the end.
0:24
If we're trying to match Google.com,
neither of these strings are valid.
0:28
We can tell the parser to only consider
these a match if the entire string
0:33
fits our pattern, and
to reject partial matches like these.
0:37
You will see these
characters often in regex's.
0:42
They are very useful in ensuring,
0:45
you are only matching at the beginning or
ending of a string.
0:47
While other regex characters
have represented characters and
0:51
strings, these only represent location.
0:54
As I mentioned in the first video,
0:59
make sure you have this checked,
match at line breaks.
1:01
This lets us treat each line as a separate
test string rather than one block.
1:05
To tell the parser to only start matching
at the beginning of test strings
1:10
I'll put a caret at
the beginning of the regex.
1:15
You see,
that excluded wwwgoogle.com because
1:19
if www is present,
it must be followed by a dot.
1:24
Now to specify the string
must end with net or
1:29
com, with only one m, I'll put a dollar
sign at the end of the expression.
1:32
Now all of the valid strings are matched.
1:38
You'll probably use a carat and dollar
sign often in your regular expressions to
1:41
eliminate strings that contain
the pattern you're matching, but
1:46
as a whole would be considered invalid.
1:49
Find some additional practice
in the teacher's notes below.
1:53
Next, let's start using everything
we've learned in a real project.
1:56
You need to sign up for Treehouse in order to download course files.
Sign up