I am new to R and regular expressions. I have been staring at these paragraphs from "R for Data Science" for hours and am still confused.
Why is there one more step in escaping "\" than "."?
1. If “.” matches any character, how do you match a literal “.”?
2. You need to use an “escape” to tell the regular expression you want to match it exactly, not use its special behaviour. Like strings, regexps use the backslash, \, to escape special behaviour. So to match an ., **you need the regexp \.**.
3. (skipped to sync with paragraphs on "\")
4. Unfortunately this creates a problem. We use strings to represent regular expressions, and \ is also used as an escape symbol in strings. So to create the regular expression \. **we need the string "\\."**.
1. If \ is used as an escape character in regular expressions, how do you match a literal \?
2. Well you need to escape it, **creating the regular expression \\**.
3. To create that regular expression, you need to use a string, which also needs to escape \. (At this point, **we have \\\**)
4. That means to match a literal \ **you need to write "\\\\"** — you need four backslashes to match one!