More on regexps

This commit is contained in:
Hadley Wickham
2022-01-12 02:08:01 -06:00
parent 8ec221c922
commit 2bc70b9c7f
2 changed files with 301 additions and 127 deletions

View File

@@ -246,6 +246,9 @@ Regular expressions are a very concise language for describing patterns in strin
Regular expressions can be overwhelming at first, and you'll think a cat walked across your keyboard.
Fortunately, as your understanding improves they'll soon start to make sense.
Regular expression is a bit of a mouthful, and the term isn't that useful as it refers to the underlying body of computer science theory where the meanings of both "regular" and "expression" are somewhat distant to their day-to-day meaning.
In practice, most people abbreviate to "regexs" or "regexps".
We'll start by using `str_detect()` which answers a simple question: "does this pattern occur anywhere in my vector?".
We'll then ask progressively more complex questions by learning more about regular expressions and the functions that use them.
@@ -401,6 +404,14 @@ str_replace_all(x, c("1" = "one", "2" = "two", "3" = "three"))
`str_remove_all()` is a short cut for `str_replace_all(x, pattern, "")` --- it removes matching patterns from a string.
`pattern` as a function.
Come back to that in Chapter \@ref(programming-with-strings).
```{r}
x <- c("1 house", "1 person has 2 cars", "3 people")
str_replace_all(x, "[aeiou]+", str_to_upper)
```
Use in `mutate()`
Using pipe inside mutate.