[R] Matching a period in grep...
Marc Schwartz
marc_schwartz at comcast.net
Wed Aug 6 04:25:05 CEST 2008
on 08/05/2008 08:55 PM Alec.Zwart at csiro.au wrote:
> Hi folks,
>
> Can anyone enlighten me as to why I get the following when I search for
> ".csv" at the end of a string?
>
>> grep("\.csv$","Blah.csv",value=TRUE)
>
> [1] "Blah.csv"
> Warning messages:
> 1: '\.' is an unrecognized escape in a character string
> 2: unrecognized escape removed from "[\.]csv$"
>
>
> R reference for regular expressions says
>
> "Any metacharacter with special meaning may be quoted by preceding it
> with a backslash. [...] The metacharacters in EREs are . \ | ( ) [ { ^
> $ * + ?"
>
> Am I missing something here? If "\." is not the right way to match a
> period, can anyone tell me what is? I can't find anything on this in R
> reference...
Look a little lower on the same page, where it notes:
Patterns are described here as they would be printed by cat: do remember
that backslashes need to be doubled when entering R character strings,
e.g. from the keyboard.
Thus:
grep("\\.csv$", "Blah.csv", value = TRUE)
Also see FAQ 7.37:
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-does-backslash-behave-strangely-inside-strings_003f
> I'm using R 2.6 on Windows XP
Time to upgrade....2.7.1 is the current version.
HTH,
Marc Schwartz
More information about the R-help
mailing list