removing part of a string

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

removing part of a string

Vito Michele Rosario Muggeo
dear all,
I am stuck on the following problem. Give a string like

ss1<- "z:f(5, a=3, b=4, c='1:4', d=2)"

or

ss2<- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"

I would like to remove all entries within parentheses.. Namely, I aim to
obtain respectively

"z:f()" or "f()*z"

I played with sub() and gsub() but without success..
Thank you very much for your time,

best,
vito


--
==============================================
Vito M.R. Muggeo
Dip.to Sc Econom, Az e Statistiche
Università di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 23895240
fax: 091 485726
http://dssm.unipa.it/vmuggeo
Associate Editor, Statistical Modelling
Chair, Statistical Modelling Society

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: removing part of a string

Rui Barradas
Hello,

Try this.


ss1 <- "z:f(5, a=3, b=4, c='1:4', d=2)"
ss2 <- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"

fun <- function(s) sub("(\\().*(\\))", "\\1\\2", s)

fun(ss1)
#[1] "z:f()"

fun(ss2)
#[1] "f()*z"


Hope this helps,

Rui Barradas

On 5/21/2018 2:33 PM, Vito M. R. Muggeo wrote:

> dear all,
> I am stuck on the following problem. Give a string like
>
> ss1<- "z:f(5, a=3, b=4, c='1:4', d=2)"
>
> or
>
> ss2<- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"
>
> I would like to remove all entries within parentheses.. Namely, I aim to
> obtain respectively
>
> "z:f()" or "f()*z"
>
> I played with sub() and gsub() but without success..
> Thank you very much for your time,
>
> best,
> vito
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: removing part of a string

Ulrik Stervbo-2
I would use

sub("\\(.*\\)", "()", s)

It is essentially the same as Rui's suggestion, but I find the purpose to
be more clear. It might also be a little more efficient.

HTH
Ulrik

On Mon, 21 May 2018, 15:38 Rui Barradas, <[hidden email]> wrote:

> Hello,
>
> Try this.
>
>
> ss1 <- "z:f(5, a=3, b=4, c='1:4', d=2)"
> ss2 <- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"
>
> fun <- function(s) sub("(\\().*(\\))", "\\1\\2", s)
>
> fun(ss1)
> #[1] "z:f()"
>
> fun(ss2)
> #[1] "f()*z"
>
>
> Hope this helps,
>
> Rui Barradas
>
> On 5/21/2018 2:33 PM, Vito M. R. Muggeo wrote:
> > dear all,
> > I am stuck on the following problem. Give a string like
> >
> > ss1<- "z:f(5, a=3, b=4, c='1:4', d=2)"
> >
> > or
> >
> > ss2<- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"
> >
> > I would like to remove all entries within parentheses.. Namely, I aim to
> > obtain respectively
> >
> > "z:f()" or "f()*z"
> >
> > I played with sub() and gsub() but without success..
> > Thank you very much for your time,
> >
> > best,
> > vito
> >
> >
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: removing part of a string

Rui Barradas
Hello,

Ulrik's way is simpler therefore better. I would use it, not mine.

As for an explanation of mine, here it goes.

1) Parenthesis are meta-characters and must be escaped if you want to
match them:

\\(  and  \\)

2) You want to keep them so I used groups, i.e., put what you want
between parenthesis (these serve a different purpose, they will not be
matched).

(\\()  and (\\))  are group \1 and group \2

3) remove everything between the two groups:

.*

4) combine all:

(\\().*(\\))  is the pattern to match

The replacement is \\1\\2

Ulrik's pattern is simpler, no groups, just \\( and \\) with .* between
them, replaced by ()

Hope this helps,

Rui Barradas

On 5/21/2018 3:00 PM, Ulrik Stervbo wrote:

> I would use
>
> sub("\\(.*\\)", "()", s)
>
> It is essentially the same as Rui's suggestion, but I find the purpose
> to be more clear. It might also be a little more efficient.
>
> HTH
> Ulrik
>
> On Mon, 21 May 2018, 15:38 Rui Barradas, <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hello,
>
>     Try this.
>
>
>     ss1 <- "z:f(5, a=3, b=4, c='1:4', d=2)"
>     ss2 <- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"
>
>     fun <- function(s) sub("(\\().*(\\))", "\\1\\2", s)
>
>     fun(ss1)
>     #[1] "z:f()"
>
>     fun(ss2)
>     #[1] "f()*z"
>
>
>     Hope this helps,
>
>     Rui Barradas
>
>     On 5/21/2018 2:33 PM, Vito M. R. Muggeo wrote:
>      > dear all,
>      > I am stuck on the following problem. Give a string like
>      >
>      > ss1<- "z:f(5, a=3, b=4, c='1:4', d=2)"
>      >
>      > or
>      >
>      > ss2<- "f(5, a=3, b=4, c=\"1:4\", d=2)*z"
>      >
>      > I would like to remove all entries within parentheses.. Namely, I
>     aim to
>      > obtain respectively
>      >
>      > "z:f()" or "f()*z"
>      >
>      > I played with sub() and gsub() but without success..
>      > Thank you very much for your time,
>      >
>      > best,
>      > vito
>      >
>      >
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list --
>     To UNSUBSCRIBE and more, see
>     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 -- To UNSUBSCRIBE and more, see
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.