0

I have the following R script in a java String variable (including new line characters):

d = 1
for (i in 1:150 ) {
 d = d + i;
}
print(d)

What I'm looking for is to obtain the R functions, regardless of new lines.

Following the previous example, I would get an array of three strings:

first: d = 1

second: for (i in 1:150 ) { d = d + i; }

third: print(d)

is this possible with Regex?

ps0604
  • 1,227
  • 23
  • 133
  • 330
  • no it is not possible. look at this https://github.com/antlr/grammars-v4/blob/master/r/R.g4 – ibre5041 Mar 06 '15 at 18:12
  • 1
    To do this right you have to recognize all valid R statements/expressions, which means you'll need to write a full parser for R and build a parse tree. Parsers may _use_ regex within them, but you can't parse this using _only_ regex. – Stephen P Mar 06 '15 at 18:17
  • The answer to this has a link to an R language parser written in C http://stackoverflow.com/questions/22028678/is-there-some-code-for-a-basic-r-parser – DwB Mar 06 '15 at 19:27
  • There are many open source parser generators available. The following link has a list of some: http://java-source.net/open-source/parser-generators – DwB Mar 06 '15 at 19:29

0 Answers0