Thanks. I converted my data structure( that is most of the confusion in my case ) into a data frame and then applied this function
y <- apply( y, 1, function(z) str_extract(z,"Current.*?[/|]")) to get "Current Usage : init:2359296, used:15857920, committed:15892480, max:50331648|" Mohan From: jim holtman <jholt...@gmail.com> To: mohan.radhakrish...@polarisft.com Cc: R mailing list <r-help@r-project.org> Date: 11/01/2013 05:17 PM Subject: Re: [R] Replace element with pattern try this: > x <- rbind("Peak Usage : init:2359296, used:15859328, committed:15892480,max:50331648Current Usage : init:2359296, used:15857920,committed:15892480, max:50331648|-------------------|") > apply(x, 1, function(a) sub("(Current.*?[/|]).*", "\\1", a)) [1] "Peak Usage : init:2359296, used:15859328, committed:15892480,max:50331648Current Usage : init:2359296, used:15857920,committed:15892480, max:50331648|" > > Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Fri, Nov 1, 2013 at 4:09 AM, <mohan.radhakrish...@polarisft.com> wrote: > Hi, > I have a data frame with one column and several rows of the form. > > "Peak Usage : init:2359296, used:15859328, committed:15892480, > max:50331648Current Usage : init:2359296, used:15857920, > committed:15892480, max:50331648|-------------------|" > > I tested the regex > > Current.*?[\|] > > in an online tester which greedily matches upto the first 'pipe' character > > Current Usage : init:2359296, used:15857920, committed:15892480, > max:50331648| > > This is what I want. > > I tried to replace the entire rows using > > apply( y, 1, function(x) gsub(x,"Current.*?[/|]",x)) which didn't work. > > How is this done ? I also want to recursively apply some more patterns one > by one on the rows till I reduce it to exactly what I want. Is there a way > to do this without loops ? > > Thanks, > Mohan > > > This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited. > > Visit us at http://www.polarisFT.com > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited. Visit us at http://www.polarisFT.com [[alternative HTML version deleted]] ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.