regex

Ch. 15

Regular Expressions

Key concepts: escaping, anchoring, character classes, shorthand classes, quantifiers, precedence and grouping.

Regex 101

This website is a great place to test your regex patterns: https://regex101.com/

Glossary

A regular expression is a tiny language of its own. These are the words you’ll need, starting with the basics.

The basics

term meaning example
string a piece of text in R, written in quotes "banana"
regular expression (regex) a pattern that describes text you’re looking for "an", "^b", "[0-9]+"
pattern the regex you give a str_*() function, usually its second argument str_detect(fruit, "berry")
match a piece of a string that fits the pattern "an" matches twice in "banana"
literal a character that matches itself: letters, digits, spaces "cat" matches the text cat
metacharacter a character with a special meaning in regex . + * ? ^ $ [ ] ( ) { } | \
case sensitive regex treats uppercase and lowercase as different letters "a" doesn’t match "A"

The key concepts

term meaning example
escaping putting a backslash before a metacharacter so it matches literally. In an R string, the backslash itself must be doubled. "\\." matches a real period; "\\$" matches a dollar sign
anchoring tying the pattern to a position instead of a character: ^ is the start of the string and $ is the end "^b" starts with b; "y$" ends with y
character class square brackets [ ] that match one character from a set. - gives a range. [aeiou] is any vowel; [0-9] is any digit
negated class a class starting with ^, which matches one character not in the set [^aeiou] is any character that isn’t a vowel
shorthand class a backslash code for a common class \\d digit, \\w word character, \\s whitespace
quantifier a symbol after something saying how many times it can repeat ? 0 or 1; + 1 or more; * 0 or more; {2} exactly 2
alternation | means “or”: match the pattern on either side "cat|dog"
precedence which parts of a pattern bind together first, like order of operations in math. Quantifiers apply only to the item right before them, and | splits the whole pattern unless you use parentheses. "ab+" is a then one or more b’s; "^a|b$" is starts with a or ends with b
grouping parentheses ( ) that treat several characters as one unit, for a quantifier or alternation "(ab)+" is ab, abab, …; "gr(a|e)y" is gray or grey
capturing and backreference a group also remembers what it matched; \\1 refers back to the first group "(..)\\1" is a pair of letters, repeated

Handy tools

term meaning example
str_view() shows the matches in each string, surrounded by < > str_view("banana", "an")
regex(..., ignore_case = TRUE) makes a pattern ignore uppercase vs. lowercase str_detect("Apple", regex("a", ignore_case = TRUE))
negate = TRUE in str_detect() and str_subset(), keeps the strings that don’t match str_subset(fruit, "e", negate = TRUE)
raw string r"(...)" writes a string without needing to double backslashes r"(\d+)" is the same as "\\d+"

Data

  1. fruit contains the names of 80 fruits.
  2. words contains 980 common English words.
  3. sentences contains 720 short sentences.

str_view()

When a second parameter is supplied, str_view() will show only the elements of the string vector that match, surrounding each match with <>, and, where possible, highlighting the match in blue.

Code
# syntax
str_view(data, "string")

The two meanings of ^

The caret ^ is the most confusing metacharacter, because it means two completely different things depending on where it appears.

where ^ appears meaning example matches
at the start of a pattern, outside [ ] the string starts here (an anchor) "^b" strings that begin with “b”
as the first character inside [ ] not any of these characters (negation) "[^b]" any one character that isn’t “b”
inside [ ], but not first a literal caret "[a^]" “a” or “^”
escaped, anywhere a literal caret "\\^" “^”

^ as an anchor: “starts with”

An anchor doesn’t match a character: it matches a position, the very start of the string. "^b" finds fruits that start with “b”. Without the anchor, "b" finds a “b” anywhere.

Code
str_view(fruit, "^b")
 [4] │ <b>anana
 [5] │ <b>ell pepper
 [6] │ <b>ilberry
 [7] │ <b>lackberry
 [8] │ <b>lackcurrant
 [9] │ <b>lood orange
[10] │ <b>lueberry
[11] │ <b>oysenberry
[12] │ <b>readfruit
Code
str_view(fruit, "b") |> head(8)
 [4] │ <b>anana
 [5] │ <b>ell pepper
 [6] │ <b>il<b>erry
 [7] │ <b>lack<b>erry
 [8] │ <b>lackcurrant
 [9] │ <b>lood orange
[10] │ <b>lue<b>erry
[11] │ <b>oysen<b>erry

Its partner is $, which anchors to the end. Together they force the pattern to match the whole string: "^a.*e$" means “starts with a, anything in between, ends with e”.

Code
str_view(fruit, "^a.*e$")
[1] │ <apple>

^ inside brackets: “not”

Inside square brackets, as the very first character, ^ flips the class: [aeiou] is any one vowel, and [^aeiou] is any one character that isn’t a lowercase vowel. Note that it still matches exactly one character.

Code
str_view("banana split", "[^aeiou]")
[1] │ <b>a<n>a<n>a< ><s><p><l>i<t>

The space was matched too: [^aeiou] means “anything except these five letters”, which includes spaces, digits, punctuation and capital letters. To match only consonants, list them, or exclude more: [^aeiou ] also excludes the space.

Both meanings in one pattern

Many useful patterns use both. "^[^aeiou]" means: at the start of the string (anchor), one character that is not a vowel (negation). In other words, fruits that start with a consonant:

Code
str_subset(fruit, "^[^aeiou]") |> head(10)
 [1] "banana"       "bell pepper"  "bilberry"     "blackberry"   "blackcurrant"
 [6] "blood orange" "blueberry"    "boysenberry"  "breadfruit"   "canary melon"

Compare that with "[^aeiou]" alone, which matches almost every fruit, because nearly every word has some consonant somewhere.

“Does not contain” is not the same as [^...]

[^e] does not mean “strings without an e”. It matches any single character that isn’t “e”, and almost every string has one of those:

Code
str_detect(c("pear", "kiwi", "eee"), "[^e]")
[1]  TRUE  TRUE FALSE

To find strings that don’t contain a pattern at all, detect the pattern and flip the result, either with ! or with negate = TRUE:

Code
str_subset(c("pear", "kiwi", "eee"), "e", negate = TRUE)
[1] "kiwi"

Examples

Code
# search for a SINGLE DIGIT number
str_detect(c("a","a1","a123","a1234"),"[0-9]")
[1] FALSE  TRUE  TRUE  TRUE
Code
str_view(c("a","a1","a123","a1234"),"[0-9]")
[2] │ a<1>
[3] │ a<1><2><3>
[4] │ a<1><2><3><4>
Code
str_replace(c("a","a1","a123","a1234"),"[0-9]","x")
[1] "a"     "ax"    "ax23"  "ax234"
Code
# search for any n-DIGIT number
str_detect(c("a","a1","a123","a1234"),"[0-9]+")
[1] FALSE  TRUE  TRUE  TRUE
Code
str_view(c("a","a1","a123","a1234"),"[0-9]+")
[2] │ a<1>
[3] │ a<123>
[4] │ a<1234>
Code
str_replace(c("a","a1","a123","a1234"),"[0-9]+","x")
[1] "a"  "ax" "ax" "ax"

Now for a TWO digit number

Code
# search for any n-DIGIT number
str_detect(c("a","a1","a123","a1234"),"[0-9]{2}")
[1] FALSE FALSE  TRUE  TRUE
Code
str_view(c("a","a1","a123","a1234"),"[0-9]{2}")
[3] │ a<12>3
[4] │ a<12><34>
Code
str_replace(c("a","a1","a123","a1234"),"[0-9]{2}","x")
[1] "a"    "a1"   "ax3"  "ax34"

Now match some date formatting. Work with the flights data.

Code
library(nycflights13) 
# grab the unique values
df <- flights |> distinct(year,month,day)

# following the principle that one should avoid typing the same thing 3 times we loop:
for (cn in names(df)){
    print(cn)
    df <- mutate(df,{{cn}} := as.character(get(cn))) |> glimpse()
}
[1] "year"
Rows: 365
Columns: 3
$ year  <chr> "2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", …
$ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ day   <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1…
[1] "month"
Rows: 365
Columns: 3
$ year  <chr> "2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", …
$ month <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",…
$ day   <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1…
[1] "day"
Rows: 365
Columns: 3
$ year  <chr> "2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", …
$ month <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",…
$ day   <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "…
Code
# now we join into a date format
  df <- df |> mutate(thedate = str_c(day,"_",month,"_",year))
  
# but that doesn't look very good, let's make everything 2 or 4 digits
# below:
# ^ anchor: look at the start of the string (outside [ ], ^ means "starts with")
# [0-9] look for any digit
# _ then look for an _
# . this is a metacharacter for any-single-character
# + this repeats the previous token as many times till the end of the string
  df <- df |> mutate(nicedate = str_replace(
    thedate,"^[0-9]_.+",str_c("0",thedate)))

# this works for some of them but not all:
  df |> arrange(nicedate)
# A tibble: 365 × 5
   year  month day   thedate   nicedate  
   <chr> <chr> <chr> <chr>     <chr>     
 1 2013  10    1     1_10_2013 01_10_2013
 2 2013  11    1     1_11_2013 01_11_2013
 3 2013  12    1     1_12_2013 01_12_2013
 4 2013  1     1     1_1_2013  01_1_2013 
 5 2013  2     1     1_2_2013  01_2_2013 
 6 2013  3     1     1_3_2013  01_3_2013 
 7 2013  4     1     1_4_2013  01_4_2013 
 8 2013  5     1     1_5_2013  01_5_2013 
 9 2013  6     1     1_6_2013  01_6_2013 
10 2013  7     1     1_7_2013  01_7_2013 
# ℹ 355 more rows

Exercises

  1. Fix the rest of the dates in nicedate
 [1] │ <01_1_>2013
 [2] │ <02_1_>2013
 [3] │ <03_1_>2013
 [4] │ <04_1_>2013
 [5] │ <05_1_>2013
 [6] │ <06_1_>2013
 [7] │ <07_1_>2013
 [8] │ <08_1_>2013
 [9] │ <09_1_>2013
[10] │ <10_1_>2013
[11] │ <11_1_>2013
[12] │ <12_1_>2013
[13] │ <13_1_>2013
[14] │ <14_1_>2013
[15] │ <15_1_>2013
[16] │ <16_1_>2013
[17] │ <17_1_>2013
[18] │ <18_1_>2013
[19] │ <19_1_>2013
[20] │ <20_1_>2013
... and 253 more
  1. Replace the date that matches your birthday with the string: It's my birthday!.

Detect Matches

str_detect() returns a logical vector that is TRUE if the pattern matches an element of the character vector and FALSE otherwise:

Code
str_detect(c("a", "b", "c"), "[aeiou]")
[1]  TRUE FALSE FALSE

Since str_detect() returns a logical vector of the same length as the initial vector, it pairs well with filter(). For example, this code finds all the most popular names containing a lower-case “x”:

Code
babynames |> 
  filter(str_detect(name, "x")) |> 
  count(name, wt = n, sort = TRUE)
# A tibble: 974 × 2
   name            n
   <chr>       <int>
 1 Alexander  665492
 2 Alexis     399551
 3 Alex       278705
 4 Alexandra  232223
 5 Max        148787
 6 Alexa      123032
 7 Maxine     112261
 8 Alexandria  97679
 9 Maxwell     90486
10 Jaxon       71234
# ℹ 964 more rows

We can also use str_detect() with summarize() by pairing it with sum() or mean():

Code
babynames |> 
  group_by(year) |> 
  summarize(prop_x = mean(str_detect(name, "x"))) |> 
  ggplot(aes(x = year, y = prop_x)) + 
  geom_line()

There are two functions that are closely related to str_detect(): str_subset() and str_which(). str_subset() returns a character vector containing only the strings that match. str_which() returns an integer vector giving the positions of the strings that match.

Show all strings that do not contain an e

The simplest way: detect “e” and keep the strings that don’t match, with negate = TRUE. (Remember from above: "[^e]" would not work, because it matches any single non-“e” character.)

Code
str_subset(fruit, "e", negate = TRUE)
 [1] "apricot"      "avocado"      "banana"       "blackcurrant" "coconut"     
 [6] "currant"      "damson"       "dragonfruit"  "durian"       "fig"         
[11] "guava"        "jackfruit"    "jambul"       "kiwi fruit"   "kumquat"     
[16] "loquat"       "mango"        "nut"          "papaya"       "passionfruit"
[21] "physalis"     "plum"         "raisin"       "rambutan"     "satsuma"     
[26] "star fruit"   "tamarillo"    "ugli fruit"  

This Stack Overflow answer does it with a single regex, using a negative lookahead (?!e), which means “the next character is not e”:

Code
# ^ and $ anchor to the start and end, so every character in between
# must pass the (?!e) check
str_subset(fruit, "^((?!e).)*$")
 [1] "apricot"      "avocado"      "banana"       "blackcurrant" "coconut"     
 [6] "currant"      "damson"       "dragonfruit"  "durian"       "fig"         
[11] "guava"        "jackfruit"    "jambul"       "kiwi fruit"   "kumquat"     
[16] "loquat"       "mango"        "nut"          "papaya"       "passionfruit"
[21] "physalis"     "plum"         "raisin"       "rambutan"     "satsuma"     
[26] "star fruit"   "tamarillo"    "ugli fruit"  

Both give the same result; the negate = TRUE version is much easier to read.

advanced regex

Count Matches

str_count(): rather than a true or false, it tells you how many matches there are in each string.

Code
x <- c("apple", "banana", "pear")
str_count(x, "p")
[1] 2 0 1

It’s natural to use str_count() with mutate(). The following example uses str_count() with character classes to count the number of vowels and consonants in each name. Here [^aeiou] uses ^ as negation (inside the brackets), not as an anchor.

Code
babynames |> 
  count(name) |> 
  mutate(
    vowels = str_count(name, "[aeiou]"),
    consonants = str_count(name, "[^aeiou]")
  )
# A tibble: 97,310 × 4
   name          n vowels consonants
   <chr>     <int>  <int>      <int>
 1 Aaban        10      2          3
 2 Aabha         5      2          3
 3 Aabid         2      2          3
 4 Aabir         1      2          3
 5 Aabriella     5      4          5
 6 Aada          1      2          2
 7 Aadam        26      2          3
 8 Aadan        11      2          3
 9 Aadarsh      17      2          5
10 Aaden        18      2          3
# ℹ 97,300 more rows

The issue: the names start with a capital letter, and [^aeiou] counts anything that isn’t a lowercase vowel, so the capital A in “Aaban” is counted as a consonant. 3 fixes:

  1. Add the uppercase vowels to the character class: str_count(name, "[aeiouAEIOU]").

  2. Tell the regular expression to ignore case: str_count(name, regex("[aeiou]", ignore_case = TRUE)).

  3. Use str_to_lower() to convert the names to lowercase: str_count(str_to_lower(name), "[aeiou]").

Choose one and resolve the issue above.

Replace Matches

As well as detecting and counting matches, we can also modify them with str_replace() and str_replace_all(). str_replace() replaces the first match, and as the name suggests, str_replace_all() replaces all matches.

Code
x <- c("apple", "pear", "banana")
str_replace_all(x, "[aeiou]", "-")
[1] "-ppl-"  "p--r"   "b-n-n-"

Extract Matches

Code
df <- tribble(
  ~str,
  "<Sheryl>-F_34",
  "<Kisha>-F_45", 
  "<Brandon>-N_33",
  "<Sharon>-F_38", 
  "<Penny>-F_58",
  "<Justin>-M_41", 
  "<Patricia>-F_84", 
)

To extract this data using separate_wider_regex() we just need to construct a sequence of regular expressions that match each piece. If we want the contents of that piece to appear in the output, we give it a name:

Code
df |> 
  separate_wider_regex(
    str,
    patterns = c(
      "<", 
      name = "[A-Za-z]+", 
      ">-", 
      gender = ".",
      "_",
      age = "[0-9]+"
    )
  )
# A tibble: 7 × 3
  name     gender age  
  <chr>    <chr>  <chr>
1 Sheryl   F      34   
2 Kisha    F      45   
3 Brandon  N      33   
4 Sharon   F      38   
5 Penny    F      58   
6 Justin   M      41   
7 Patricia F      84   

Exercises

Practice from the text

Regex Tidbits

  1. Raw strings:
Code
# more intuitive way to define a weird string
s <- r"{"'\?}"

More Exercises

15.4.7

Practice