[R] yet another regular expression

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Jun 30 16:15:18 CEST 2014


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.



More information about the R-help mailing list