[R] regular expression

Laurent Rhelp laurentRhelp at free.fr
Sun Apr 8 10:10:22 CEST 2007


Gabor Grothendieck a écrit :

> I can't reproduce that error.  For me it works fine.  What version of
> R and gsubfn are you using?  I switched to a French locale just to
> be sure.  I tried pasting it into an R session and also sourcing
> it but both worked fine for me.
>
>> Sys.getlocale()
>
> [1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
>
>>
>> Sys.putenv("LANGUAGE"="FR");Sys.setlocale("LC_ALL","FR")
>
> [1] 
> "LC_COLLATE=French_France.1252;LC_CTYPE=French_France.1252;LC_MONETARY=French_France.1252;LC_NUMERIC=C;LC_TIME=French_France.1252" 
>
>
>> library(gsubfn)
>>
>> squote <- "'" # single quote. This is a double quote, single quote, 
>> double quote
>> dquote <- '"' # double quote  This is a single quote, double quote, 
>> single quote
>>
>> s <- "[\"...\" \"...'...\"]"
>>
>> f <- function(x)  chartr(paste(squote, dquote), paste(dquote, 
>> squote), x)
>> gsubfn('["][^"]*["]', f, s)  #   "['...' '...\"...']"
>
> [1] "['...' '...\"...']"
>
>> R.version.string # XP
>
> [1] "R version 2.4.1 Patched (2006-12-30 r40331)"
>
>> packageDescription("gsubfn")$Version
>
> [1] "0.3-3"
>
> On 4/8/07, Laurent Rhelp <laurentRhelp at free.fr> wrote:
>
>> Gabor Grothendieck a écrit :
>>
>> >On 4/7/07, Laurent Rhelp <laurentRhelp at free.fr> wrote:
>> >
>> >
>> >>Gabor Grothendieck a écrit :
>> >>
>> >>
>> >>
>> >>>I assume is to replace single quotes in double quoted strings with 
>> single quoted
>> >>>strings containing double quotes in place of the single quotes
>> >>>
>> >>>
>> >>>
>> >>that's rignt
>> >>
>> >>
>> >>
>> >>>while leaving
>> >>>double quoted strings not containing single quotes as they are 
>> (i.e. leave them
>> >>>as double quoted).
>> >>>
>> >>>
>> >>>
>> >>No, at the end,  I have to replace all the double quotes (") by simple
>> >>quote (')
>> >>
>> >>
>> >
>> >In that case it simplifies to the following (same as before except
>> >for definition of f):
>> >
>> >library(gsubfn)
>> >
>> >squote <- "'" # single quote. This is a double quote, single quote, 
>> double quote
>> >dquote <- '"' # double quote  This is a single quote, double quote, 
>> single quote
>> >
>> >s <- "[\"...\" \"...'...\"]"
>> >f <- function(x)  chartr(paste(squote, dquote), paste(dquote, 
>> squote), x)
>> >gsubfn('["][^"]*["]', f, s)  #   "['...' '...\"...']"
>> >
>> >______________________________________________
>> >R-help at stat.math.ethz.ch mailing list
>> >https://stat.ethz.ch/mailman/listinfo/r-help
>> >PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> >and provide commented, minimal, self-contained, reproducible code.
>> >
>> >
>> >
>> >
>> I have the following error message :
>>
>>  > gsubfn('["][^"]*["]', f, s)  #   "['...' '...\"...']"
>> Erreur dans parse(file, n, text, prompt) : erreur de syntaxe dans
>> "c("[\",replacement(""...\"""
>>
>>
>>
>
>
I am sorry Gabor, I had the R 2.3.1 version. I downloaded the good 
versions (R and gsubfn). All works fine now.
Thank you very much for your nice solution !



More information about the R-help mailing list