The basic issue is that regular expressions are handled by functions in R, they aren't a built-in part of the language. Building them in would require a change in the way characters are parsed when reading R code. Since regular expressions aren't central to the language, this is seen as an unnecessary complication.
More specifically, for the R parser to handle regex(\.)
, you'd need a new reserved word (regex
), and a whole new parsing mode to be defined, with its own complications. For example, both ""
and ")"
are legal regular expressions. (Ignore the quotes, just consider the characters within them.) Putting them in your suggested syntax would look like regex()
and regex())
, so the R parser would have to look ahead when it hit the first )
to know where the regular expression ended. But "))"
is also legal, so how would it know where to stop?
Putting regular expressions into strings adds the extra layer of escapes, but at least it doesn't complicate the design of the parser.
EDITED TO ADD:
As of R 4.0.0, things are better for writing regular expressions because of the new syntax for string literals described in this NEWS entry:
There is a new syntax for specifying raw character constants similar
to the one used in C++: r"(...)" with ... any character sequence not
containing the sequence )". This makes it easier to write strings that
contain backslashes or both single and double quotes. For more details
see ?Quotes.
So if you want to enter \.
, you replace the ...
above with exactly what you want, with no escapes necessary:
r"(\.)"
This is parsed the same as "\.". It's not exactly what you wished for, but it's kind of close.