Mention character class instead of escaping

@jennybc
This commit is contained in:
Hadley Wickham 2022-11-09 14:37:55 -06:00
parent 899c1dce85
commit 4a761c77c6
1 changed files with 9 additions and 0 deletions

View File

@ -350,6 +350,14 @@ That lets you to avoid one layer of escaping:
str_view(x, r"{\\}")
```
If you're trying to match a literal `.`, `$`, `|`, `*`, `+`, `?`, `{`, `}`, `(`, `)`, there's an alternative to using a backslash escape: you can use a character class: `[.]`, `[$]`, `[|]`, \...
all match the literal values.
```{r}
str_view(c("abc", "a.c", "a*c", "a c"), "a[.]c")
str_view(c("abc", "a.c", "a*c", "a c"), ".[*]c")
```
The full set of metacharacters is `.^$\|*+?{}[]()`.
In general, look at punctuation characters with suspicion; if your regular expression isn't matching what you think it should, check if you've used any of these characters.
@ -885,3 +893,4 @@ You'll find stringi very easy to pick up because it follows many of the the same
In the next chapter, we'll talk about a data structure closely related to strings: factors.
Factors are used to represent categorical data in R, data where there is a fixed and known set of possible values identified by a vector of strings.