Intermediate Regular Expressions in R
Angelo Zehr
Data Journalist
pattern = "Nemo|Harmony|Dory"
can also be created like this:
names <- c("Nemo", "Harmony", "Dory")
pattern = glue_collapse(names, sep = "|")
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,… |
Multiplier | Repetitions |
---|---|
+ |
One or more repetitions |
* |
Zero or more repetitions |
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