Introducing regular expressions
A regular expression is a sequence of characters that bear a special meaning and are used for pattern matching in strings. Since the specific meaning of characters in a regular expression requires some memorization and can easily be forgotten if you do not use them often, we will avoid introducing its underlying syntax and focus on intuitive and more human-friendly programming using the rebus
package. It is a good companion to stringr
and provides utility functions that facilitate string manipulation and make building regular expressions much easier. Remember to install this package via install.package("rebus")
when you use it for the first time.
The rebus
package has a special operator called %R%
that’s used to concatenate matching conditions. For example, to detect whether a string starts with a particular character, such as s
, we could specify the pattern as START %R% "s"
and pass it to the pattern argument of the str_detect...