[R] regex question

Gabor Grothendieck ggrothendieck at gmail.com
Tue Nov 4 02:54:30 CET 2008


If patReg1 and patReg2 are your two regex's then:

   gsub(paste(patReg1, patReg2, sep = "|"), "", varReg)


On Mon, Nov 3, 2008 at 8:37 PM, Ferry <fmi.mlist at gmail.com> wrote:
> hello,
>
> i am trying to extract text using regex as follows:
>
> "*  < <* this is my text > > "
>
> into:
>
> "this is my text"
>
> below what I did:
>
> varReg <- "*  < <* this is my text > > "
>
> ## either this pattern
> patReg <- "(^[ <*]+)"
> ## or below patten
> patReg <- "([ > ]+$)"
>
> sub(patReg, '', varReg)
>
> depending of which patten I use, I could only extra the first portion
> or the last portion of the unwanted characters. how to extract both
> ends and keep my text "this is my text" ?
>
> I have tried with gsub, as below:
> patReg <- "([ >* ]+)"
> gsub(patReg, '', varReg)
>
> but it returned "thisismytext"
>
> any idea is appreciated.
>
> thanks,
>
> ferry
>
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list