[R] Parsing regular expressions differently - feature request

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Nov 9 00:22:16 CET 2008


Wacek Kusnierczyk wrote:
> Duncan Murdoch wrote:
>   
>>
>> That might solve John's problem, but I doubt it.  As far as I can see
>> it won't handle \L, for example.
>>
>>     
>
> well, it was not supposed to.  it addresses the need for doubling
> backslashes when a backslash character is an element of the regex. 
>
> foo = "foo\\n\n"
>
> grep("\n", foo, perl=TRUE, value=TRUE)
> mygrep("\n", foo, perl=TRUE, value=TRUE)
> # both match the newline
>
> grep("\\n", foo, perl=TRUE, value=TRUE)
> mygrep("\\n", foo, perl=TRUE, value=TRUE)
> # both match (guess what)
>
> bar = "bar\n"
>
> grep("\n", bar, perl=TRUE, value=TRUE)
> mygrep("\n", bar, perl=TRUE, value=TRUE)
> # both match the newline
>
> grep("\\n", bar, perl=TRUE, value=TRUE)
> mygrep("\\n", bar, perl=TRUE, value=TRUE)
> # counterintuitively, grep matches (intuitively, it should match
> backslash-n, not a newline, but there's just a newline in bar) -- i do
> know why it matches, but i'm pretty sure for many of those who do it's
> an inconvenient detail, and for those who don't it's a confusing annoyance
>
> zee = "zee\\"
>
> grep("\\", zee, perl=TRUE, value=TRUE)
> mygrep("\\", zee, perl=TRUE, value=TRUE)
> # grep fails, needs "\\\\"
>
> conclusion?  i'd opt for mygrep in my own code; i guessed this was what
> john wanted, therefore the post.
>
> vQ
>   

here's another example of what could be considered r grep's idiosyncrasy:

grep("\\n", "\n", perl=TRUE)
# matches

grep("\\n", "\\n", perl=TRUE)
# matches

with everything else equal, "\\n" should match *either* newline *or*
backslash-n, no?

vQ



More information about the R-help mailing list