[R] gsub
peter dalgaard
pdalgd at gmail.com
Thu Jun 14 15:38:49 CEST 2012
On Jun 14, 2012, at 13:03 , nalluri pratap wrote:
>
> Hi,
>
> I have a string t1="(Ithis) I(test)". I need to get t2="(Ithis) test".
>
> Can someone look into this. ?
Well, you can, it is your problem....
You need to look into the hairier parts of regular expression syntax. Looks like the problem is to find the pattern I(XXXX) and replace with XXXX. For that you need to use a back-reference (\1). Something like pattern: I\((.*)\) replacement: \1
The tricky bit is to remember whether it is the "real" parentheses that need escaping or the ones used for grouping; and, in R, to remember to escape all backslashes in the regexps.
Lessee....
> gsub("I\\((.*)\\)", "\\1", "(Ithis) I(test)")
[1] "(Ithis) test"
> I have tried using gsub("I[^)]","",t1) , but didn't get the required result.
>
> Thanks,
> Pratap
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
--
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list