Ranges

There is a better way! With the yearPattern, we matched any digit from 0 to 9 using [0-9], which is way easier than typing out [0123456789], although they both do the same thing. Within brackets, a dash between two characters is called a range. Ranges are most commonly used for ranges of numbers and letters. Ranges on other characters can be quite confusing, and I don’t recommend using them. You can match any upper- or lowercase letter in the English alphabet using [a-zA-Z]. That is much better (and you don’t have to worry that you missed one of the letters). Ranges don’t have to start with 0 or A, and they don’t have to end with 9 or Z. If you want to match only letters in the second half of the alphabet, you can use [n-z], for instance. ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.