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
Matching any character EXCEPT a specific character can be very useful at times. Learn how.
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:
foxes
jumping
dogs
Exclude:
aaaaa
2 )
Match:
34522
72379
Exclude:
1234k
5784k
5784k
3 )
Match *only* the commas:
pears,
apples,
cherries,
oranges,
4 )
Match only the text character strings from each line ('html', 'head', etc):
<html>
<head>
</head>
<body>
<div>
</div>
</body>
</html>
You can exclude any character or
characters from a match.
0:00
This can be useful for
matching text between the limiters.
0:04
For comma separated values, for example,
0:09
we could match any character that is not
a comma, to pull the limited values out.
0:12
To match any character
except a target character,
0:18
we use a character set
that starts with a carat.
0:21
For example,
to match any character except an @ symbol,
0:25
you can put an @ symbol next
to a carat in a character set.
0:30
Add a dot to that to exclude
dots from what is matched.
0:36
Let's try this out.
0:41
I cleared both windows so
that we can start with a new example.
0:43
Let's put an email address in as a test,
say, toy@boat.com.
0:46
Now I'll match any character
that is not an @ symbol, not at.
0:53
I'll put square brackets with a carat and
an @ symbol.
1:01
This striping means that each character
is a separate, complete match for
1:06
our expression.
1:10
Every character except for
the @ symbol, that is.
1:12
If we want to match more than just
one character with our expression,
1:15
what should we do?
1:19
Do you remember?
1:21
One way would be to put a plus after it.
1:23
Now we have two complete matches.
1:27
The parser found three characters
that were not @ symbols,
1:30
then it found an @ symbol.
1:35
So toy is the first match for
this expression.
1:37
After the @ symbol it found
boat.com was a repeated string of
1:41
characters that weren't assembled.
1:46
If I put a dot in the set, You see
the string has three complete matches.
1:50
There are a couple of gotchas
I want to point out here.
1:58
Notice that a dot inside a set behaves
differently than a dot outside a set.
2:01
Inside it's a little dot,
2:08
while outside it's a special
character that matches everything.
2:10
This can take some getting used to,
but just remember, context matters.
2:15
Next think about what would
happen if I removed the + and
2:21
put an m before the character set.
2:26
Do you think it will match
the m at the end of the string?
2:30
Because m isn't followed by an @ or a dot.
2:34
Turns out, the m is not matched.
2:41
This is because the m isn't
followed by any character at all.
2:44
The character set is telling the parser
that m must be followed by a character.
2:49
It just can't be an @ symbol or a dot.
2:55
So if I put a character here,
say a %, we have a match.
2:59
Regular expressions include characters,
which are the opposite of the digit,
3:07
word and whitespace characters
that we learned earlier.
3:11
If you capitalize any of them,
you get the inverse.
3:15
So, \D, for example,
3:19
matches any character that is not a digit.
3:22
For example, if I erase the regex, And
3:27
put a \W, the @ symbol and
the dot are matched,
3:32
because they're not word characters.
3:37
If I put a capital D instead,
3:41
each character is matched because
none of them are numerals.
3:43
Check the teacher's notes for
additional practice.
3:50
You need to sign up for Treehouse in order to download course files.
Sign up