Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spliting the character into parts

Tags:

r

substr

gsub

grepl

I observe the following character:

  l <- "mod, range1 = seq(-m, n, 0.1), range2 = seq(-2, 2, 0.1), range3 = seq(-2, 2, 0.1)"

Using regular expressions in R I desire to split l in the following structure:

[1] "mod"                      "range1 = seq(-m, n, 0.1)"
[3] "range2 = seq(-2, 2, 0.1)" "range3 = seq(-2, 2, 0.1)"

Unfortunetely, I didn't find a proper way to overcome the problem, yet. Anyone has an idea how is it possible to acquire such an elegeant split?

like image 329
And_R Avatar asked Dec 14 '22 05:12

And_R


1 Answers

I really doubt you can do it with regular expression. You are trying to parse your string and so you need a parser, which is generally more powerful than a regex. I don't think it's general enough, but you can take advantage of the R parser and the alist class. Try:

res<-eval(parse(text=paste0("alist(",l,")")))
paste0(names(res),ifelse(names(res)!="","=",""),as.character(res))
#[1] "mod"                    "range1=seq(-m, n, 0.1)" "range2=seq(-2, 2, 0.1)"
#[4] "range3=seq(-2, 2, 0.1)"

Keep in mind that the regex proposed solutions fail if there are nested brackets. Try them and mine with:

l<-"mod, range1 = seq(-m, n, 0.1), range2 = seq(-2, exp(2), 0.1), range3 = seq(-2, 2, 0.1)"

to understand what I mean.

like image 142
nicola Avatar answered Jan 03 '23 18:01

nicola