6.10 Regexp Pattern Matching

20180608 One of the most powerful string processing concepts is the concept of regular expressions. A regular expression is a sequence of characters that describe a pattern. The concept was formalised by the American mathematician Stephen Cole Kleene. A regular expression pattern can contain a combination of alphanumeric and special characters. It is a complex topic and we take an introductory look at it here to craft regular expressions in R.

Visit RegExr, by G Skinner to explore regular expressions interactively.

An important concept is that of metacharacters which have special meaning within a regular expression. Unlike other characters that are used to match themselves, metacharacters have a specific meaning beyond the character they represent. The following table contains a list of common metacharacters used in regular expressions.

% latex table generated in R 4.6.1 by xtable 1.8-4 package % Tue Sep 1 17:06:36 2026

Such metacharacters are used to match different patterns which can be found using base::grep(). According to <gnu.org/software/grep> g/re/p is a command from the command line tool ed to get the regular expression and print it.

s <- c("hands", "data", "on", "data$cience", "handsondata$cience", "handson")
grep(pattern="^data", s, value=TRUE)
## [1] "data"        "data$cience"
grep(pattern="on$", s, value=TRUE)
## [1] "on"      "handson"
grep(pattern="(nd)..(nd)", s, value=TRUE)
## [1] "handsondata$cience"

In order to match a metacharacter in R we need to escape it with \(\backslash\backslash\) (double backslash).

grep(pattern="\\$", s, value=TRUE)
## [1] "data$cience"        "handsondata$cience"


If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0