Hi all, I have managed, with the help of glob2rx() to get two parts of a text manipulation working. I have successfully gotten rid of the first and second bits, but I have hit the wall trying to get rid of the last bit. Here's an example: initString<- "\"Delete this\":value1,\"Delete this too\":value2},Delete last bit" sub("\"Delete this too\":","", sub(glob2rx("*this\":*"),"",initString),fixed=TRUE) This gives me: [1] "value1,value2},Delete last bit" and glob2rx("},*") just won't get rid of the last bit. I throw myself upon the mercy of the regular expression gurus. Jim
If everything Regular Expressions could do could be done with globbing then RE would likely not exist. I have interpreted your problem as: Start at the beginning of the string ("^") match stuff that is not a colon ("[^:]") match a colon (":') keep stuff that is not a comma ("([^,]*)") match a comma (",") keep stuff that is not a right brace ("([^}]*)") match a right brace and comma ("},") match any characters (".*") up to the end of the string ("$") and then substitute that whole matched string with the first kept match ("\\1") a comma (",") the second kept match ("\\2") which gives sub("^[^:]*:([^,]*),[^:]*:([^}]*)},.*$","\\1,\\2",initString) The separator comma could be wrapped with the first and second kept matches in this case to make a single kept match.. I was just methodically working my way through your pattern finding pieces of interest. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On June 30, 2014 6:13:18 AM PDT, Jim Lemon <jim at bitwrit.com.au> wrote:>Hi all, >I have managed, with the help of glob2rx() to get two parts of a text >manipulation working. I have successfully gotten rid of the first and >second bits, but I have hit the wall trying to get rid of the last bit. >Here's >an example: > >initString<- > "\"Delete this\":value1,\"Delete this too\":value2},Delete last bit" >sub("\"Delete this too\":","", > sub(glob2rx("*this\":*"),"",initString),fixed=TRUE) > >This gives me: > >[1] "value1,value2},Delete last bit" > >and glob2rx("},*") just won't get rid of the last bit. I throw myself >upon >the mercy of the regular expression gurus. > >Jim > >______________________________________________ >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.
Hi Jim What about trying to use what you want to keep m=gregexpr('\\":value[12]+[}]?,', initString) regmatches(initString, m) [[1]] [1] "\":value1," "\":value2}," Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Lemon Sent: Monday, 30 June 2014 23:13 To: r-help at r-project.org Subject: [R] yet another regular expression Hi all, I have managed, with the help of glob2rx() to get two parts of a text manipulation working. I have successfully gotten rid of the first and second bits, but I have hit the wall trying to get rid of the last bit. Here's an example: initString<- "\"Delete this\":value1,\"Delete this too\":value2},Delete last bit" sub("\"Delete this too\":","", sub(glob2rx("*this\":*"),"",initString),fixed=TRUE) This gives me: [1] "value1,value2},Delete last bit" and glob2rx("},*") just won't get rid of the last bit. I throw myself upon the mercy of the regular expression gurus. Jim ______________________________________________ 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.
Hi Jim, May be this helps: library(stringr) paste(str_extract_all(initString, perl('(?<=\\:)[[:alnum:]]+,?'))[[1]],collapse="") #[1] "value1,value2" A.K. On Monday, June 30, 2014 9:58 AM, Jim Lemon <jim at bitwrit.com.au> wrote: Hi all, I have managed, with the help of glob2rx() to get two parts of a text manipulation working. I have successfully gotten rid of the first and second bits, but I have hit the wall trying to get rid of the last bit. Here's an example: initString<- "\"Delete this\":value1,\"Delete this too\":value2},Delete last bit" sub("\"Delete this too\":","", sub(glob2rx("*this\":*"),"",initString),fixed=TRUE) This gives me: [1] "value1,value2},Delete last bit" and glob2rx("},*") just won't get rid of the last bit. I throw myself upon the mercy of the regular expression gurus. Jim ______________________________________________ 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.
On Mon, 30 Jun 2014 11:13:18 PM Jim Lemon wrote:> Hi all, > I have managed, with the help of glob2rx() to get two parts of a text > manipulation working. I have successfully gotten rid of the first and > second bits, but I have hit the wall trying to get rid of the last bit. > Here's an example: > > initString<- > "\"Delete this\":value1,\"Delete this too\":value2},Delete last bit" > sub("\"Delete this too\":","", > sub(glob2rx("*this\":*"),"",initString),fixed=TRUE) > > This gives me: > > [1] "value1,value2},Delete last bit" > > and glob2rx("},*") just won't get rid of the last bit. I throw myself upon > the mercy of the regular expression gurus. >Hi again, Sorry, but my "minimal reproducible example" was apparently misleading. "value1" and "value2" are not fixed strings, but varying numbers. After a night's sleep I solved the problem using strsplit(), which gave me a big list of strings that I could process without wearing out the "\" key. Thanks for the suggestions. Jim