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

Ottorino-Luca Pantani ottorino-luca.pantani at unifi.it
Mon Nov 16 15:54:17 CET 2009


Duncan Murdoch ha scritto:
> 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.
>
> Duncan Murdoch
>> Any of these (the need for doubling of the "\\" for the 
>> back-reference seems to be the main issue:
>> > gsub("_([[:digit:]])_.", "_0\\1_", foo)
>>  
>> > gsub("_(\\d)_.", "_0\\1_", foo)
>>  
>> > gsub("V_(.)_", "V_0\\1_", foo)
>>
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>>
>
I suspected something on the double escape..........

Thanks to you all.
R is a wonderful software and R-help is always a great place to visit !!!
8rino




More information about the R-help mailing list