Skip to content

: regular expressions: escaping a dot

5 messages · wolfram at fischer-zim.ch, Tobias Verbeke, Peter Dalgaard +1 more

#
What's really the problem with:
Warning: '\.' is an unrecognized escape in a character string
	Warning: unrecognized escape removed from "\.odt$"
	[1] 5
	attr(,"match.length")
	[1] 4

I know that I could use:
But it seems to me that the first expression is also
an accepted regular expression in accordance with perl.

Regards - Wolfram
#
Wolfram Fischer wrote:

            
In R you have to escape the "\".

 From the help page of regexpr:

## Double all 'a' or 'b's;  "\" must be escaped, i.e., 'doubled'
gsub("([ab])", "\\1_\\1_", "abc and ABC")

HTH,
Tobias
#
On Thu, 28 Jun 2007, Wolfram Fischer wrote:

            
This is explained in ?regexp (in the See Also of ?regexpr):

      Patterns are described here as they would be printed by 'cat': _do
      remember that backslashes need to be doubled when entering R
      character strings from the keyboard_.

and in the R FAQ and ....
#
Prof Brian Ripley wrote:
Hmm, that's not actually correct, is it? Perhaps this is better

"...entering R character string literals (i.e., between quote symbols.)"

The counterexample would be
\\abc
[1] "\\\\abc"

(of course it is more important to get people to read the documentation
at all...)
#
On Thu, 28 Jun 2007, Peter Dalgaard wrote:

            
The definition of 'character string' used throughout the help is your 
'character string literal', as distinct from an element of a character 
vector.