Skip to content

Splitting a string expression into components

4 messages · Daniel Murphy, William Dunlap, Bert Gunter +1 more

#
If your syntax is like R's syntax then parse() can help.
  R> p <- parse(text="z = 1 + 2")
  R> p[[1]] # the first (& only) expression in "z = 1 + 2"
  z = 1 + 2
  R> as.list(p[[1]]) # the 'function' called (`=`) and its 'arguments'
  [[1]]
  `=`
  
  [[2]]
  z
  
  [[3]]
  1 + 2
  
  R> as.list(p[[1]][[3]]) # ditto for the 2nd argument above
  [[1]]
  `+`

  [[2]]
  [1] 1
  
  [[3]]
  [1] 2

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
Alternatively, perhaps strsplit() with an appropriate regex argument.

See ?strsplit ?regexp

e.g.

strsplit ("z<=5",split = "[<>=]+")
[[1]]
[1] "z" "5"

This of course will only work if you know what the characters are that
define "left" and "right", how complex the expressions might be, etc.
-- i.e. the simple case.

-- Bert
On Wed, Jul 3, 2013 at 10:55 AM, William Dunlap <wdunlap at tibco.com> wrote:

  
    
4 days later