Matching Specific Characters4:36 with Joel Kraft
Learn how to write simple regular expressions with optional characters.
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'd most likely glance at them both and say, hm, yes. 0:03 However, a regular expression doesn't work that way. 0:08 Instead, it uses a parser which compares each character in a regular 0:11 expression with a character in the string in the same position. 0:16 In other words, the regex parser requires two things. 0:20 An expression, also called a pattern, and a string to match. 0:23 The parser takes that pattern and walks through the string, 0:28 character by character. 0:31 First, it checks if there is a t. 0:33 Then if the t is followed by an o. 0:36 Which is followed by a y, then a b, o, 0:39 a, all the way to the t at the end. 0:43 At that point, the parser knows the string is a match. 0:47 I'm going to type toyboat into regexpal, in the regex pane, in the testing pane. 0:51 You can see there is a match selected here. 0:58 This is the basic matching we saw earlier. 1:01 Let's say we want to match the singular or the plural. 1:04 If I add an s to the regex, we lose our first match. 1:09 However, we can make this final s optional. 1:14 Match it if it exists but don't require it. 1:18 We can do that with another special regex character, the question mark. 1:21 The question mark matches the character that appears directly before it 1:25 zero times or one time. 1:29 In this case, the lower case s is before the question mark, so s is optional. 1:32 But if it does appear, it can only appear once. 1:38 So this regex will match toyboat or toyboats but not toyboatss with two S's. 1:41 Let's use the question mark in the middle of the string. 1:48 Let's match toybots, too. 1:50 We can put a question mark after the a to make it optional also. 1:53 It works. 1:57 I'll replace toybots with toyboats again, but this time with a space. 2:00 Just like before, 2:05 we can handle this with a question mark, making the space optional. 2:06 Now let's say that we want to match toy, even if it's capitalized. 2:14 Regular expressions are case sensitive by default, so 2:20 we'll need to specify that the T can be upper or lower case. 2:23 We can tell the interpreter to accept a capital or 2:28 lower case t by wrapping both characters in square brackets. 2:31 Let me show you what I mean. 2:36 I'll put a capital T next to the lowercase t, and surround both with square brackets. 2:38 I just added what is called a character set to the regex. 2:46 When the interpreter sees it, 2:50 exactly one of the characters in the set will match one position in the string. 2:51 We can specify as many characters as we want between these brackets. 2:57 Let's say we also want to match joy boats with a lowercase j, for example. 3:01 We can just put a j inside the square brackets. 3:06 There's two things to note. 3:10 No matter how many characters we put in the set, 3:11 the set will always match only one position of the string. 3:14 In this case, it's the first position. 3:18 It also makes no difference how you order the character set. 3:21 Now I wanted to give you a challenge. 3:24 I'll type out two more test cases for you. 3:26 See if you can modify the regex to match those 3:29 along with the strings it already matches. 3:32 I'll add a hyphenated version. 3:35 And another version with the B capitalized. 3:44 Go ahead and pause the video and see if you can do it. 3:48 For the hyphenated string, I replaced the space and 3:52 the regex with a character set containing a space and a hyphen. 3:55 Note that the question mark now indicates that the character set is optional. 4:02 In other words, the character between the y and the b can either be a space, 4:08 a hyphen, or nothing at all. 4:13 For the second string, 4:16 I used a character set to match either a lower or upper case B. 4:17 I've added some additional strings for 4:24 you to practice on in the teachers notes below. 4:26 Take some time to try then now if you'd like. 4:28 In the next video, I'll show you how to add a little more power to character sets. 4:32
You need to sign up for Treehouse in order to download course files.Sign up