Using Wildcard Characters3:51 with Joel Kraft
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 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.
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 wild card matches more than one character in a string. 0:00 While we've been using character sets to match more than one character, 0:05 there are shorter ways to match some cases. 0:08 For example, instead of using [0-9] to match any numeral, use [SOUND] \d. 0:11 The backslash is found often in regular expressions, but 0:18 its meaning can differ according to where it's used. 0:21 Usually, it's a way to say that the character that follows 0:24 has a special meaning. 0:27 In this case, it means the d isn't the letter d, but 0:29 a special instruction to match a numeral. 0:33 Even though there are two characters there, a backslash and 0:36 a d, the parser treats this as a single character. 0:39 To match all alphanumeric characters, as well as an underscore, use \w. 0:44 This can be handy for matching what's known as word characters. 0:49 The reason the underscore is included is that it's often used to space words 0:54 out that can't actually have a space character in them. 0:58 For example, some file systems won't let you use spaces in file names. 1:01 So it's become common to use underscores instead. 1:06 [SOUND] To match white space characters, [SOUND] you can use \s. 1:09 In addition to the space, this wild card includes other kinds of white space, 1:14 a tab and a few kinds of line return characters. 1:19 If you want to know more about these special characters, 1:22 check out the teacher's notes, but for now just think of \s as 1:25 a way to match white space, such as spaces, tabs and new lines. 1:29 Also the period or dot matches any character. 1:34 Let's try some of these out. 1:39 Let's use a digit character shorthand in place of the character set 1:40 at the beginning. 1:44 Now any digit will be matched but we lost the match we had of the letter A. 1:47 We could match that by using a word shorthand like so. 1:52 Now any letter or digit will match. 1:56 I'll change the first line to an uppercase A And it still matches. 1:59 Note an underscore will also match. 2:04 Often when you're composing reg xs, you'll want to control what character you include 2:08 and while the word shorthand will often work, you might 2:13 find yourself going with something more limiting like what we had before. 2:16 We can include these shorthands inside character sets. 2:21 If I create a character set with a digit shorthand and 2:25 an a, the expression matches the top two strings again. 2:28 I'll replace this space character with a \s. 2:33 Now if I add an 8 and a space to the next line, and 2:39 then replace this space with a tab, the string still matches. 2:44 I'll add a 3 to the next line and 2:49 replace this space with a new line character by pressing Enter. 2:52 See how the color carries over? 2:59 So these two lines are one match now. 3:01 Let's use the dot. 3:04 I'll replace the S with a dot. 3:06 All the same matches are still there, 3:09 because the dot will match any character at all. 3:10 Let's change one of these Ss to be an exclamation point, for example. 3:13 Or a dollar sign. 3:19 As you can see, the dot is a powerful character in regexs. 3:21 You'll see it in a lot of examples around the web. 3:25 Remember that the question mark matches zero or one character. 3:28 In other words, the character is optional. 3:32 I'll show you another example of where you might use this in the next tab. 3:35 You could use a question mark to match both American and 3:39 British spellings of the word color, like this. 3:42 Check the teacher's notes for some additional practice. 3:45 In the next video, I'll show you how to match repeating characters. 3:47
You need to sign up for Treehouse in order to download course files.Sign up