0

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!
Barmar
  • 741,623
  • 53
  • 500
  • 612
Steve
  • 193
  • 8
  • The string is being processed twice: first as a string literal in the programming language, then as a regular expression. Each step treats backslash as an escape character, and converts two consecutive backslashes to a single backslash. – Barmar Jun 18 '20 at 23:36
  • Thanks! I have read the associated question before I post this question. However, I will read it again in details. – Steve Jun 19 '20 at 03:07

0 Answers0