Matching Character Ranges1:58 with Joel Kraft
Learn match ranges of characters such as a-z, A-Z or 0-9.
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 characters like A to Z, or 0:00 a range of numbers like zero to nine in character sets. 0:04 Let's say instead of matching just these three characters for example, 0:07 you want to match any character in the alphabet. 0:12 This can be done by replacing the contents of the brackets with A-Z. 0:15 This hyphen tells the parser to include all letters in the range from A to Z. 0:20 You can see below that only one of the strings is highlighted. 0:24 That's because we've only specified uppercase characters. 0:30 To include all lower case letters, we can put another range right next to the first. 0:33 So ranges are put right next to each other in the square brackets. 0:39 Notice, all the strings are highlighted again. 0:43 That's because the ranges we just entered include the ts and 0:46 js these strings start with. 0:50 I'll just change one of these ts to a z. 0:52 It's still matched. 0:57 Now, I'll change it to a capital S, cool. 0:58 I'll change it back to a t. 1:01 I just wanna show you that you can use numerals the same way. 1:04 I'll create a new character set at the beginning of the expression, 1:08 followed by a space. 1:12 Inside I'll put 0 through 7. 1:14 All our tests are unmatched now because the parser is looking for 1:16 the string to start with a numeral between 0 and 7. 1:19 I'll type a 2 on the second line, followed by a space, and it's matched. 1:22 I'll replace it with a 9. 1:30 And it's unmatched. 1:33 Note, I can still enter individual characters I want to match in a set. 1:35 For example, let's say I want to match the letter a. 1:39 I can type it in the set and then use it on the first line. 1:43 It matches. 1:48 Try some of the exercises in the teacher's notes for more practice. 1:49 Next, let's look at more concise ways to express common character sets. 1:53
You need to sign up for Treehouse in order to download course files.Sign up