[R] on gsub (simple, but not to me!) sintax

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Nov 16 15:12:15 CET 2009


Duncan Murdoch wrote:
> On 11/16/2009 8:21 AM, Ottorino-Luca Pantani wrote:
>> Dear R users,
>> my problem today deals with my ignorance on regular expressions.
>> a matter I recently discovered.
>
> You were close.  First, gsub by default doesn't need escapes before 
> the parens.  (There are lots of different conventions for regular 
> expressions, unfortunately.)  So the Emacs regular expression V_\(.\)_ 
> is entered as "V_(.)_" in the default version of gsub().  Second, to 
> enter a backslash into a string, you need to escape it.  So the 
> replacement pattern V_0\1_ is entered as "V_0\\1_".  So
>
> gsub("V_(.)_", "V_0\\1_", foo)
>
> should give you what you want.

actually, guessing from the form of the input, sub is more appropriate, 
though the performance gain seems inessential (~3%).

vQ




More information about the R-help mailing list