Ultimate cheatsheet for regex in R

Regular expressions are one of those topics programmers tend to either love or hate. And I admit, sometimes it’s confusing. In order to structure the information, I made an overview. So in this blog post I will share the ultimate cheatsheet for using regex in R!

Regular expressions are also called regex or regexp. A regex is a text string that defines a search pattern. Regex can be used to manipulate and extract information from text strings. Regex are universally supported din many programming languages like R, Python, Java and SQL.

While regex are universally supported, there are some slight differences when using regex in different programming languages. I made a cheatsheet with examples that can specifically be used in R. Base R has several function available for searching patterns in a string:

  • grepl()
  • grep()
  • sub()
  • gsub()
  • regexpr()
  • gregexpr()
  • regexec()

These functions  allow you to search for matches to the argument pattern within each element of a character vector. They differ in the format of and amount of detail in the results.

But how do we define the pattern? That’s where the ultimate cheatsheet for regex in R comes in!

I was not particularly happy when using regex, but this ultimate cheatsheet for regex in R made it a lot easier.

A few tips when using regex:

  • Start small. Break large regex down if necessary.
  • Comment your regex. Nobody wants to figure out a monstrous 20-line regex.


I hope you like it and find it useful for future reference!

If you want to receive the cheatsheet in high quality PDF you can send me an e-mail and I will send you a copy.

Want to learn more about regex? I recommend using this excellent reference.  


Leave a Reply

Your email address will not be published. Required fields are marked *