Matching Character Ranges2:33 with Alena Holligan
Learn match ranges of characters such as a-z, A-Z or 0-9.
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.
bat cat data eat fast
1 a 2 a 3 a 4 a 5 a 8 a 9 b
8345 Gable 7238 Gable 2349 Table 8475 Cable 0994 Fable 1047 Zable
4849 4472 4901 40502 43470 40496
You can also match a range of letters, like A through Z, or 0:00 a range of numbers, like 0 through 9, in character sets. 0:05 Let's say, instead of the first set that matches just these three characters, 0:10 you want to match any letter in the alphabet. 0:15 This can be done by replacing the contents of the brackets. 0:18 We'll set A-Z. 0:25 This hyphen tells the parser to include all letters in the range from A to Z. 0:29 You can see below that only one of the strings is highlighted. 0:36 That's because we've only specified uppercase letters. 0:39 To include all lowercase letters, 0:43 you can put another range right next to the first, lowercase a-z. 0:46 So ranges are put right next to each other in the square brackets. 0:53 Notice all the strings are highlighted again. 0:58 That's because the ranges we just entered include the Ts and 1:00 the Js that these strings start with. 1:05 I'll just change one of these Ts to a Z. 1:09 It still matches. 1:13 Now I'll change it to a capital S. 1:16 Cool, it still matches. 1:18 I'm going to change it back to the t. 1:21 I also want to show you that you can use numerals in the same way. 1:23 I'll just create a new character set at the beginning of the expression. 1:28 In square brackets, followed by a space, I'm going to add 0 through 7. 1:34 All of our tests are unmatched now because the parser is looking for 1:41 the string to start with a numeral between 0 and 7. 1:46 I'll type a 2 on the second line, followed by a space, and it's matched again. 1:50 I'll replace it with a 9, and it's unmatched. 1:59 Note that I can still enter individual characters I want to match into the set. 2:04 For example, let's say that I want to match the letter a. 2:09 I can type that into the set and then use it in our first line. 2:15 It matches. 2:21 Try some of the exercises in the teachers notes for more practice. 2:23 Next, we'll look at a more concise way to express common character sets. 2:28
You need to sign up for Treehouse in order to download course files.Sign up