yet another regular expression

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

yet another regular expression

Jim Lemon
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: yet another regular expression

Jeff Newmiller
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:<[hidden email]>        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 <[hidden email]> 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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: yet another regular expression

Duncan Mackay-4
In reply to this post by Jim Lemon
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: [hidden email]

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On
Behalf Of Jim Lemon
Sent: Monday, 30 June 2014 23:13
To: [hidden email]
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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: yet another regular expression

arun kirshna
In reply to this post by Jim Lemon
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 <[hidden email]> 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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: yet another regular expression

Jim Lemon
In reply to this post by Jim Lemon
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

______________________________________________
[hidden email] 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.