Hello,
I am trying to remove brackets and the text contained in brackets. I tried
to do a user defined formula... my attempt at this is pasted below.
cleanBetweenBrackets <- function(String)
{ return(gsub("\(.*?\)", "", String))}
I keep getting errors (namely that there is an unrecognised escape
character in the string). I have looked at regex forums a bit, but cant
figure this out.
I want the above formula to be able to produce the following result
>Str<-"The cat is crazy (but not too crazy)"
>StrNoBrackets<-cleanBetweenBrackets(Str)
>StrNoBrackets
[1] "The cat is crazy "
Assistance would be appreciated,
Audrey
[[alternative HTML version deleted]]
Hello,
I have been able to figure this out using \\ (two back slashes for escape)
Working R code for what I wanted is...
cleanBetweenBrackets <- function(String)
{ return(gsub("\\(.*?\\)", "", String))}
I thought I had tried that (the \\) before I emailed the list. Please
ignore my previous email.
Sorry for any inconvenience caused.
Audrey
---------- Forwarded message ----------
From: Audrey Riddell <audreykaspi at gmail.com>
Date: Tue, Sep 6, 2016 at 4:56 PM
Subject:
To: r-help at r-project.org
Hello,
I am trying to remove brackets and the text contained in brackets. I tried
to do a user defined formula... my attempt at this is pasted below.
cleanBetweenBrackets <- function(String)
{ return(gsub("\(.*?\)", "", String))}
I keep getting errors (namely that there is an unrecognised escape
character in the string). I have looked at regex forums a bit, but cant
figure this out.
I want the above formula to be able to produce the following result
>Str<-"The cat is crazy (but not too crazy)"
>StrNoBrackets<-cleanBetweenBrackets(Str)
>StrNoBrackets
[1] "The cat is crazy "
Assistance would be appreciated,
Audrey
[[alternative HTML version deleted]]
R's implementation of regex requires double backslashes. Reading
?regex will tell you more.
cleanBetweenBrackets <- function(String)
{
return(gsub("\\(.*?\\)", "", String))
}
Str <- "The cat is crazy (but not too crazy)"
cleanBetweenBrackets(Str)
> cleanBetweenBrackets(Str)
[1] "The cat is crazy "
The trailing space is left as an exercise for the reader.
Sarah
On Tue, Sep 6, 2016 at 2:56 AM, Audrey Riddell <audreykaspi at gmail.com>
wrote:> Hello,
>
>
> I am trying to remove brackets and the text contained in brackets. I tried
> to do a user defined formula... my attempt at this is pasted below.
>
> cleanBetweenBrackets <- function(String)
> { return(gsub("\(.*?\)", "", String))}
>
> I keep getting errors (namely that there is an unrecognised escape
> character in the string). I have looked at regex forums a bit, but cant
> figure this out.
>
> I want the above formula to be able to produce the following result
>
>>Str<-"The cat is crazy (but not too crazy)"
>>StrNoBrackets<-cleanBetweenBrackets(Str)
>>StrNoBrackets
> [1] "The cat is crazy "
>
> Assistance would be appreciated,
>
> Audrey
>
--
Sarah Goslee
http://www.functionaldiversity.org
It is not the implementation of regex that requires double backslashes, but the R string parser. You can use cat to see what the pattern looks like to the parser. Try cat( "\\(.*?\\)" ) -- Sent from my phone. Please excuse my brevity. On September 6, 2016 6:33:15 AM PDT, Sarah Goslee <sarah.goslee at gmail.com> wrote:>R's implementation of regex requires double backslashes. Reading >?regex will tell you more. > > >cleanBetweenBrackets <- function(String) >{ > return(gsub("\\(.*?\\)", "", String)) >} > >Str <- "The cat is crazy (but not too crazy)" > >cleanBetweenBrackets(Str) > >> cleanBetweenBrackets(Str) >[1] "The cat is crazy " > > >The trailing space is left as an exercise for the reader. > >Sarah > >On Tue, Sep 6, 2016 at 2:56 AM, Audrey Riddell <audreykaspi at gmail.com> >wrote: >> Hello, >> >> >> I am trying to remove brackets and the text contained in brackets. I >tried >> to do a user defined formula... my attempt at this is pasted below. >> >> cleanBetweenBrackets <- function(String) >> { return(gsub("\(.*?\)", "", String))} >> >> I keep getting errors (namely that there is an unrecognised escape >> character in the string). I have looked at regex forums a bit, but >cant >> figure this out. >> >> I want the above formula to be able to produce the following result >> >>>Str<-"The cat is crazy (but not too crazy)" >>>StrNoBrackets<-cleanBetweenBrackets(Str) >>>StrNoBrackets >> [1] "The cat is crazy " >> >> Assistance would be appreciated, >> >> Audrey >>