More on regexps
This commit is contained in:
11
strings.Rmd
11
strings.Rmd
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user