What is the simplest way to extract a matched subexpression? Eg. in perl you can do "hello world" =~ m/hello (.*)/ which would return 1(true) and set $1 to the matched subexpression "world". -- View this message in context: http://n4.nabble.com/regular-expression-submatch-tp1459146p1459146.html Sent from the R help mailing list archive at Nabble.com.
Hi, On Mon, Feb 1, 2010 at 1:57 PM, sjaffe <sjaffe at riskspan.com> wrote:> > What is the simplest way to extract a matched subexpression? > > Eg. in perl you can do > > "hello world" =~ m/hello (.*)/ > > which would return 1(true) and set $1 to the matched subexpression "world".Read through the documentation and examples in the gregexpr help page (?gregexpr from the R prompt) -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
On Feb 1, 2010, at 1:57 PM, sjaffe wrote:> > What is the simplest way to extract a matched subexpression? > > Eg. in perl you can do > > "hello world" =~ m/hello (.*)/ > > which would return 1(true) and set $1 to the matched subexpression > "world".If you wanted a logical value returned, then ?grep If you want the matched subexpression, use , value=TRUE. ??"regular expressions"> > -- > View this message in context: http://n4.nabble.com/regular-expression-submatch-tp1459146p1459146.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
Try this: gsub("hello (.*)", "\\1", "hello world") On Mon, Feb 1, 2010 at 4:57 PM, sjaffe <sjaffe at riskspan.com> wrote:> > What is the simplest way to extract a matched subexpression? > > Eg. in perl you can do > > "hello world" =~ m/hello (.*)/ > > which would return 1(true) and set $1 to the matched subexpression "world". > > -- > View this message in context: http://n4.nabble.com/regular-expression-submatch-tp1459146p1459146.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Try this:> library(gsubfn) > strapply("hello world", "hello (.*)")[[1]][1] "world" On Mon, Feb 1, 2010 at 1:57 PM, sjaffe <sjaffe at riskspan.com> wrote:> > What is the simplest way to extract a matched subexpression? > > Eg. in perl you can do > > "hello world" =~ m/hello (.*)/ > > which would return 1(true) and set $1 to the matched subexpression "world". > > -- > View this message in context: http://n4.nabble.com/regular-expression-submatch-tp1459146p1459146.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Thanks for the suggestions. gsub("hello (.*)", "\\1", "hello world") seems simplest. Setting value=TRUE returns the whole match, not the subexpression. (I always read the man pages carefully before asking for help, gratuituous comments notwithstanding. I didn't see a solution using gregexpr; if the poster wants to point out my oversight, he is welcome to do so.) -- View this message in context: http://n4.nabble.com/regular-expression-submatch-tp1459146p1459215.html Sent from the R help mailing list archive at Nabble.com.