Using Wildcard Characters4:49 with Alena Holligan
Learn some shortcut characters for some common character sets.
Copy both the Match and the Exclude set of test strings from each exercise below into 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.
7849 3472 8901 70502 23470 90496
1 a 2 a 3 a 4 a 5 a 8 a 9 b
4 c 4 E
8345 Gable 7238 Gable 2349 Table 8475 Cable 0994 Fable 1047 Zable
5633 gable jklh 3able
bat cat data eat fast 12_6
A regular expression wildcard matches more than one character in a string. 0:00 While we've been using character sets to match one than one character, 0:05 there are shorter ways to match some of these cases. 0:09 For example, instead of using 0 through 0:12 9 to match any numeral, use the \d. 0:17 The backslash is found often in regular expressions, but 0:22 its meaning can differ, according to where it's used. 0:25 Usually it's a way to say that the character that follows has a special 0:29 meaning. 0:33 In this case, it means that the d Isn't the letter d, 0:33 but a special instruction to match a numeral. 0:38 And even though there are two characters there, a backslash and 0:43 a d, the parser treats this as a single character. 0:48 To match all alphanumeric characters as well as an underscore, use \w. 0:52 This can be handy for matching what's known as word characters. 1:00 The reason underscore is included is that it's often used to space words 1:05 out that can't actually have a space character in them. 1:10 For example, some file systems won't let you use spaces in file names, 1:13 so it's become common practice to use underscores instead. 1:19 To match whitespace characters, you can use \s. 1:23 In addition to the space, this wildcard includes other kinds 1:28 of whitespace, a tab and a few kinds of line returns. 1:33 If you want to know more about these special characters, 1:37 check the Teacher's Notes. 1:40 But for now, just think of the \s as a way to match whitespace, 1:41 such as spaces, tabs, and new lines. 1:47 Also, the period or dot matches any character. 1:50 Let's try some of these out. 1:55 Let's use a digit character shorthand in place of the first set, \d. 1:58 Now any digit will be matched, but we lost the match we had for the letter a. 2:06 We could match that by using a word shorthand, like this. 2:12 Now any letter or digit will match. 2:18 I'll change this first line to use a capital A, and it still matches. 2:22 Note that an underscore will also match. 2:28 Often when you're composing regex, you'll want to control what you include though. 2:33 While the shorthand will often work, you might find yourself going with 2:39 something more limiting, like we had before. 2:43 We'll use \da, but we'll put these in a character set. 2:48 Now the top string is no longer matched because it's an underscore. 2:55 But if I change it back to an a, the first two lines are matched. 2:59 I'll replace this space with a \s. 3:03 Now notice, if I add an 8 and then a space, 3:08 and then change the second space to a tab, 3:13 the string still matches. 3:18 I'll add a 3 to the next line, And 3:21 instead of a space, I'll create a new line by typing Enter. 3:26 See how the color carries over? 3:31 So these two lines are one match now. 3:33 Let's use the dot. 3:36 I'll replace the s with a dot. 3:38 All the same matches are still there because the dot will 3:43 match any character at all. 3:46 Let's change one of these s's to be an exclamation point, for example. 3:48 It's a match, or maybe a dollar sign. 3:56 As you can see, the dot is a powerful character in regex. 4:00 You'll see it used in a lot of examples around the web. 4:04 Remember that the question mark matches zero or one character. 4:07 In other words, the character is optional. 4:12 I'll show you another example where you might use this in a new tab. 4:16 You could use the question mark to match both the American and 4:23 the British spellings of the word, like color. 4:27 Check the notes for some additional practice. 4:41 In the next video, I'll show you how to match repeating characters. 4:44
You need to sign up for Treehouse in order to download course files.Sign up