Re: [R] regular expression

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 07 Apr 2007 - 19:07:38 GMT

I assume is to replace single quotes in double quoted strings with single quoted strings containing double quotes in place of the single quotes while leaving double quoted strings not containing single quotes as they are (i.e. leave them as double quoted). Is that right? In that case, try this:

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) if (regexpr(squote, x) > 0)

   chartr(paste(squote, dquote), paste(dquote, squote), x) else x gsubfn('["][^"]*["]', f, s) # "[\"...\" '...\"...']"

One could omit the first and last pair of [] in the last line but I put them in for readability.

On 4/6/07, Laurent Rhelp <laurentRhelp@free.fr> wrote:
> Dear R-List,
>
> I have a great many files in a directory and I would like to
> replace in every file the character " by the character ' and in the same
> time, I have to change ' by '' (i.e. the character ' twice and not the
> unique character ") when the character ' is embodied in "....."
> So, "....." becomes '.....' and ".....'......" becomes '.....''......'
> Certainly, regular expression could help me but I am not able to use it.
>
> How can I do that with R ?
>
> Thank you very much
>
> ______________________________________________
> R-help@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.
>



R-help@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. Received on Sun Apr 08 05:12:34 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 07 Apr 2007 - 20:33:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.