[R] Regexpr with "."

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed Aug 13 16:54:49 CEST 2003

Thompson, Trevor wrote:

> It looks like R is treating every character in the string as if it were
> decimal.  I didn't see anything in the help file about "." being some kind
> of special character.  Any idea why R is treating a decimal this way in
> these functions?   Any suggestions how to get around this?

'.' is the regexpr character for matching any single character!

 > regexpr("a.e", "Female.Alabama")
[1] 4

  To actually search for a dot, you need to 'escape' it with a 
backslash, but of course the backslash needs escaping itself, with 
another backslash. Luckily that backslash doesn't need escaping, 
otherwise we would quickly run out of patience.

 > regexpr("\\.", "Female.Alabama")
[1] 7


More information about the R-help mailing list