Video: Negative character sets

We now know how to write character sets, which essentially say to match any one character that's inside the set. Now we're going to see how to write a negative character set, which is to match any one character which is not in the set. The way we're going to do that is by using a new metacharacter, which is the caret. It looks like a little up arrow. Take a moment and locate that on your keyboard. For most of you, it's probably over the 6 key. And the caret is going to have a couple of meanings in regular expressions. This is going to be meaning number one, right.

Learn how to find and manipulate text quickly and easily using regular expressions. Author Kevin Skoglund covers the basic syntax of regular expressions, shows how to create flexible matching patterns, and demonstrates how the regular expression engine parses text to find matches. The course also covers referring back to previous matches with backreferences and creating complex matching patterns with lookaround assertions, and explores the most common applications of regular expressions.

Negative character sets

We now know how to write character sets, which essentially say to match any onecharacter that's inside the set.Now we're going to see how to write a negative character set, which is to matchany one character which is not in the set.The way we're going to do that is by using a new metacharacter, which is the caret.It looks like a little up arrow. Take a moment and locate that on your keyboard.For most of you, it's probably over the 6 key. And the caret is going to have acouple of meanings in regular expressions.This is going to be meaning number one, right.

This is only inside the character set as the first character of the character set.So we'll come back and learn the other meanings of the caret later, but rightnow we're dealing with it only in the context of character sets.So what it means is "not any one of several characters." So we put it as thefirst character inside the character set and that then negates the character set;it's then the opposite.It still represents just one character though; don't let that confuse you either.So for example, if we had not aeiou, then it matches any one consonant, any onething that's not a vowel-- something that's not aeiou.

See how that works? It just negates it.So if we have see not M or N then that matches seek, because K is not M or N, andit matches sees, because S is not M or N, but it does not match seem or seen,because those do end in M or N. The first three letters do match and then thatcharacter set is things that should not match.Anything except these. Not only these, but except these.Now use caution when you're working with these negations because in thatexample of M and N it does match SEE with a space after it, because space is acharacter, so that does match. But it does not match SEE with no space after it.

If it was the end of the line or the end of the file, then we just had SEE, wellthen it would say oops!That's not a match because I was expecting to still have one character, right.So there still needs to be a character present, and the character may be a white-space even, but there must still be a character present. Let's try these out.And inside the test data I'm going to paste in the string.Now we know how to make negative character sets.So inside here we're going to make a character set.Now let's put the negative in there, and then let's just slowly type the lettersA through Z with all lowercase.

So abcdefghijklmnopqrstuv. See how those disappear? Watch the Ws at the beginning.W, it says, not w; w is not allowed. X, y, and z.So now the only thing left is the N, the spaces, and the period; that's all.Now if I put a space in there, it takes away those. I can put in a period, and let'sdo then capital N. Now it doesn't match any of them.

Now it says, all right, match anything that it is not of these characters.Well, all those characters have been included. See how that works?Of course, it's the same thing if we use a range, characters a-z, and let's goahead and say A-Z as well.So anything that's not lowercase a-z or capital A-Z, the not applies toeverything that's in this whole character set.So it's now a negative character set.We're not just negating this portion of it, we're negating the entire character set.Let's try another one. Let's put up here aeiou, and then for the expression here, I'm going to type, "Itseems I see the sea I seek."Notice that each letter highlighted only once, right.

It's still only a single character, just like a regular character set.If you turn off global here then you can really see it right. It's just a single letter.So let's keep this text down here, and now let's try a new regex up here.Actually, let's put seem to start with--that matches seem.Let's don't match the M by putting the negative in there, right, and let's goahead and tell it not to match the N either. That's what we had in our actual example.So now notice that it still does match SEE space, and the space is included inthe match, and it also matches seek.

All right, now let's just take away the period and the K. Notice now that seedoes not match, right. If I put a space after it, it does, but if I have nocharacter after it, it doesn't.See how that works?So that's the key things.It's the first character of the character set, the negation applies to theentire character set, and it still matches one character. That character must bepresent. That character may be a space, may be punctuation, right.Put a period after it. Then it would still match, but it has to be one character still.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.