Get startedGet started for free

Regular expressions

1. Regular expressions

Regular expressions are a language for

2. Regular expressions

describing patterns in strings. A regular expression is just a sequence of characters, but the language assigns special meaning to some characters. Take a look at this regular expression. It describes the pattern "the start of the string, followed by any single character, followed by one or more digits". In this regular expression the carat, the dot, the square brackets, the backslash d and the plus all have special meaning. Learning regular expressions involves learning what these special characters mean and the rules for putting them together. You can see that the description of the pattern is very concise, but if you haven't seen one before, rather cryptic. Regular expressions can be

3. Regular expressions as a pattern argument

used in any stringr function that has a pattern argument. If you want to specify one directly, you specify it as a string. Because back slashes have special meaning in R strings, you have to escape them, with another backslash, to get the correct regular expression:You won't have to worry about this because you'll be constructing regular expressions using the rebus package. Take a look at the same regular expression written using rebus:The special operator %R%, is rebus's way to combine parts of a regular expression, and you should just read it as "then" or "followed" by. We have START followed by ANY_CHAR followed by one_or_more digits. Although writing the regular expressions with rebus is a little more verbose, it's a lot closer to the language _you_ would use to describe the pattern, this makes it easier to understand, even by someone who doesn't know regular expressions. rebus also takes care of any escaping

4. Regular expressions as a pattern argument

so you can provide the result directly to a stringr function: I think you'll find using rebus a gentle way to start using regular expressions in R, but I'll also highlight the raw regular expressions language, and you'll learn enough that you'll also be able to attack regular expression in other languages too. The most useful stringr function, as you start to learn regular expressions, is str_view. It takes a string, and a pattern, and will open an HTML view with any matches to the pattern highlighted. There is an additional argument match that if you set to TRUE will only display strings with matches, this can be useful when you have a lot of strings and don't want to see the ones that don't have any matches. Hmmm. looks like only one of these is the droid we are looking for. Ready to get started?

5. Let's practice!

You'll learn about specifying the start and end of the string and any character in the following exercises.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.