Viewers: in countries Watching now:
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.
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.
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 right now 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 the first 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 one thing 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, and it 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 that character 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 that example of M and N it does match SEE with a space after it, because space is a character, 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, well then 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 letters A 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's do 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 go ahead and say A-Z as well. So anything that's not lowercase a-z or capital A-Z, the not applies to everything 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, "It seems 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 go ahead 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 in the match, and it also matches seek.
All right, now let's just take away the period and the K. Notice now that see does not match, right. If I put a space after it, it does, but if I have no character 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 the entire character set, and it still matches one character. That character must be present. 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.
A character set always matches one character.
There are currently no FAQs about Using Regular Expressions.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.