I'm trying to figure out how to get the text captured by capturing groups out of a regex match. For instance, let's say I have the pattern "foo ([^ ]+)" and I match it against the string "This is a foo sentence I am reading." The group in the pattern will match the word "sentence" in the target string. How can I get access to this matched group? All I can seem to get the various grep/gsub functions to do is return or modify the entire target string. Isn't there a way to extract ONLY the text from a particular group or groups? Thanks, -- --OKB (not okblacke) Brendan Barnwell "Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail." --author unknown
Is this what you want:> x <- "This is a foo sentence I am reading." > # only return the desired match > sub(".*foo ([^ ]+).*", "\\1 <file://0.0.0.1/>", x)[1] "sentence"> >On Sun, May 2, 2010 at 6:03 PM, OKB (not okblacke) <brenbarn@brenbarn.net>wrote:> I'm trying to figure out how to get the text captured by capturing > groups out of a regex match. For instance, let's say I have the pattern > "foo ([^ ]+)" and I match it against the string "This is a foo sentence > I am reading." The group in the pattern will match the word "sentence" > in the target string. How can I get access to this matched group? All > I can seem to get the various grep/gsub functions to do is return or > modify the entire target string. Isn't there a way to extract ONLY the > text from a particular group or groups? > > Thanks, > -- > --OKB (not okblacke) > Brendan Barnwell > "Do not follow where the path may lead. Go, instead, where there is > no path, and leave a trail." > --author unknown > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
The strapply function in gsubfn does that. See http://gsubfn.googlecode.com On Sun, May 2, 2010 at 6:03 PM, OKB (not okblacke) <brenbarn at brenbarn.net> wrote:> ? ? ? ?I'm trying to figure out how to get the text captured by capturing > groups out of a regex match. ?For instance, let's say I have the pattern > "foo ([^ ]+)" and I match it against the string "This is a foo sentence > I am reading." ?The group in the pattern will match the word "sentence" > in the target string. ?How can I get access to this matched group? ?All > I can seem to get the various grep/gsub functions to do is return or > modify the entire target string. ?Isn't there a way to extract ONLY the > text from a particular group or groups? > > Thanks, > -- > --OKB (not okblacke) > Brendan Barnwell > "Do not follow where the path may lead. ?Go, instead, where there is > no path, and leave a trail." > ? ? ? ?--author unknown > > ______________________________________________ > 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. >