1. Shortcuts
Imagine you want to specify a dollar sign followed by a digit.
2. Ranges in character classes
You could do something like: a dollar sign followed by the character class that includes the numbers zero through 9.Because specifying a digit like this is so common, regular expressions provide a couple of shortcuts. The first thing you should know is that you can specify a range using dash inside a character class. For example, specifying 0 dash 9 will match any digit between 0 and 9 inclusive. Similarly, "a" dash "z" will match any lower case letter, and capital "A" dash capital "Z" will match any upper case letter. Then there are even shorter shortcuts!
3. Shortcuts
A digit, the DGT constant in rebus, or backslash "d" in regex, specifies any digit and is equivalent to the character class of the range zero though nine. A word character can be specified with WRD in rebus or \w in regex and is equivalent to any lower case or upper case letter, a digit or underscore,And a white space character can be specified with SPC in rebus or \s in regex and will match any single space, tab or newline character. All these shortcuts also have negated versions, that is "match a character that isn't a digit, a word character or a whitespace character". In rebus there are NOT_DGT, NOT_WRD and NOT_SPC constants, and they correspond to backslash capital "D", backslash capital "W" and backslash capital "S" in regex. You now have most of the basics of regular expressions. Just by combining the pieces you've learnt so far, you'll be able to match some pretty complicated patterns. You'll practice the shortcuts you just learnt in the next exercise, but afterwards we have a more complicated task for you. I've extracted some narratives from
4. National Electronic Injury Surveillance System (NEISS)
data in the neiss package. This data comes from the National Electronic Injury Surveillance System, or NEISS for short, a database of all injuries associated with consumer products from a sample of US hospital emergency rooms. Here's an example of a narrative string:Your task will be to combine your stringr and regex skills to extract
5. National Electronic Injury Surveillance System (NEISS)
the age and gender information from the string.
6. Let's practice!