Gluing regular expressions

Intermediate Regular Expressions in R

Angelo Zehr

Data Journalist

Collapsing with the pipe

pattern = "Nemo|Harmony|Dory"

can also be created like this:

names <- c("Nemo", "Harmony", "Dory")
pattern = glue_collapse(names, sep = "|")
Intermediate Regular Expressions in R

A quick refresh

Character Class Name Example
\\d Digit 0, 1, 2, 3,…
\\w Word a, b, c…, 1, 2, 3…, _
\\s Space " ", tabs and line breaks
[A-Za-z] Letter A, B, C,…, a, b, c,…
Intermediate Regular Expressions in R

A quick refresh

Multiplier Repetitions
+ One or more repetitions
* Zero or more repetitions
Intermediate Regular Expressions in R

Break up complex patterns

api_response <- "payload: 'Adam, 5, 3', headers: 'Auth...'"

str_match(api_resopnse, pattern = "[A-Za-z]+, \\d+, \\d+")

Will match: Adam, 5, 3

pattern = glue_collapse(c(
  "name" = "[A-Za-z]+",
  ", ",
  "attempts" = "\\d+",
  ", ",
  "logins" = "\\d+"
))
Intermediate Regular Expressions in R

Let's practice!

Intermediate Regular Expressions in R

Preparing Video For Download...