Matching Specific Characters5:09 with Alena Holligan
Learn how to write simple regular expressions with optional characters.
Copy each set of test strings into regex101. Using what you've learned so far, create a regular expression that will match all of the strings in the set.
ladybug ladybugs lady bugs
ladybug lady bugs lazy bug lazy lug
ladybug lazy lug lazy slug hazy slug
ladybug fading rug!
If I ask you whether these two words match, 0:00 you'll most likely glance at them both and say yes. 0:03 However, a regular expression doesn't work that way. 0:06 Instead, it uses a parser, which compares each character in a regular expression 0:09 with the character in the string in the same position. 0:14 In other words, the RegeEx parser requires two things, 0:18 an expression, also called a pattern, and a string to match. 0:22 The parser takes that pattern and 0:27 walks through the string, character by character. 0:29 First it checks if there is a t, then if the t is followed by an o and so on. 0:33 [SOUND] It's a match. 0:39 I'm going to type the word toyboat into RegEx 101 in the RegEx pane, 0:45 and then the testing pane. 0:50 You can see that there's a match selected here. 0:54 This is the basic match we saw earlier. 0:58 Let's say we want to match the singular or the plural version. 1:01 If I add an s in the RegEx, we lose our first match. 1:08 However, we can make this final s optional, 1:14 match it if it exists, but don't require it. 1:19 We can do that with another special RegEx character, the question mark. 1:22 The question mark matches the character that appears directly before it, 1:27 zero or one time. 1:33 In this case, the lower case s is before the question mark. 1:35 So s is optional. 1:40 But if it does appear, it can only appear once. 1:43 So this RegEx will match toyboat or toyboats, 1:46 but not toyboats with two s. 1:51 Let's use a question mark in the middle of the string. 1:55 Let's match toybots as well. 1:58 We can put a question mark after the a to make it optional also. 2:06 It works. 2:11 I'll replace toybots with toyboats again. 2:13 But I'm also going to add one with a space, toy boats. 2:18 Just like before, we can handle this with a question mark. 2:23 Now let's say we want to match toy, even if it's capitalized. 2:31 Toy boats. 2:39 Regular expressions are case sensitive by default, so 2:42 we'll need to specify that the T can be uppercase or lowercase. 2:45 We can tell the interpreter to accept a capital or 2:51 lowercase T by wrapping both characters in square brackets. 2:54 Let me show you what I mean. 2:59 I'll put a capital T next to the lowercase t, and 3:02 I'll surround them both in square brackets. 3:06 I just added what is called a character set to the RegEx. 3:09 When the interpreter sees it, 3:14 exactly one of the characters in the set will match one position in the string. 3:16 We can specify as many characters as we want between these brackets. 3:21 Let's say we also want to match joy boats, with a lowercase j. 3:26 I'll add joy boats. 3:34 There's two things to note. 3:38 No matter how many characters we put in the set, 3:39 the set will match only one position of the string. 3:42 In this case, it's the first position. 3:46 It also makes no difference how you order the character set. 3:48 Now I want to give you a challenge. 3:53 I'll type out two more test cases for you. 3:55 See if you can modify the RegEx to match those as well. 3:58 I'll add a hyphenated version, toy-boats, 4:02 and I'll also add one with a capital B. 4:08 Go ahead and pause this video and see if you can do it. 4:14 For the hyphenated string, I'll replace the space in the RegEx 4:18 with a character set containing both a space and a hyphen. 4:23 Note that the question mark now indicates that the character set is optional, 4:30 in other words, the character between the y and 4:35 the b can either be a space, a hyphen, or nothing at all. 4:39 For the second string, 4:45 I use a character set to match either a lower or upper case B. 4:47 I've added some additional strings for you to practice in the teacher's notes. 4:55 Take some time now to give them a try. 5:00 In the next video, I'll show you how to add a little more power to character sets. 5:03
You need to sign up for Treehouse in order to download course files.Sign up