Groups3:08 with Joel Kraft
Grouping characters allows you to apply modifications to multiple characters the way you have learned to do with single characters.
Copy both the Match and the Exclude set of test strings from each exercise below into a Regex tester like regexpal or 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.
img_01.jpg img_02.png img_03.gif img_04.png img_05.gif img_06.jpg
projector protractor proctor
img_sm_01.jpg img_01.jpg img_sm_02.jpg img_02.jpg img_sm_03.jpg img_03.jpg img_sm_04.png img_04.png
www.github.com github.com www.teamtreehouse.com teamtreehouse.com api.github.com
Most of the regex wildcards I've shown you are used to match single characters. 0:00 But what if you want to match groups of characters? 0:05 You can use parentheses to group parts of expressions together. 0:08 Let's look at how we can use grouping to avoid the repetition in the regular 0:12 expression from the last video. 0:16 If I put the three alternate expressions into a group, we can separate that 0:18 part of the expression that matches the same characters, the space boat. 0:24 Now the grouped part of the expression can be thought of as one unit. 0:38 It matches one of three possible substrings toy, sale or tug. 0:42 The test string still has to end with space boat to be matched though. 0:47 Here's a similar challenge. 0:52 Write an expression to match google.com and 0:54 google.net but not google.org. 1:00 You can copy these three test strings from the teacher's notes below. 1:05 Pause the video, give it a try and then I'll show you how I did it. 1:09 Each string begins the same way, namely, Google.. 1:13 I'll start by typing that, then I can group Com 1:19 pipe net after that, to match only the top two strings. 1:24 One thing to note, though. 1:30 While this expression works for 1:32 these strings, the dot could get us into trouble. 1:33 Remember, it's a wild card character, so it would match dollar sign, too, for 1:37 example. 1:42 If we want to specify that it only matched dot, we can add a back slash before it. 1:43 This is called escaping because we're escaping its normal behavior 1:50 to get an alternate behavior. 1:54 Now, this back slash dot is considered by the parser to be a literal dot character. 1:57 You may have wrapped the dot in square brackets for 2:02 your solution, which works just fine. 2:05 Using a backslash is a little more concise though. 2:07 So I wanted to make sure you knew about it. 2:10 Grouping let's us treat more complex patterns like letters. 2:13 In other words we can make them optional with a question mark or 2:17 allow repetitions with the plus or star, for example. 2:21 Let's add the string www.google.com. 2:25 To include this entire string in our matches, we could type www\., 2:31 but putting a question mark after the dot will only make the dot optional. 2:37 In other words, wwwgoogle.com is also a match. 2:44 We want everything before it to be optional as well, not just the dot. 2:52 If I group the pattern, the group becomes optional. 2:57 Try a few of the exercises in the teacher's notes for 3:03 more practice with groups. 3:06
You need to sign up for Treehouse in order to download course files.Sign up