You can do (string
being your string):
gsub("((\\S+)|(\\{[^{}]+\\}))\\s", "\\1,", trimws(gsub("[^[:alnum:].{}]+", " ", string)))
Explanation:
gsub("[^[:alnum:].{}]+", " ", string)
: replace everything that is not an alphanumeric character or a curly bracket or a dot (and which can occur multiple times) by a space
trimws(...)
: remove the leading and trailing spaces from the modified string you just got
gsub("((\\S+)|(\\{[^{}]+\\}))\\s", "\\1",...)
: in the former result, capture everything that is before a space and composed of non spaces or anything in between curly brackets and put a comma after.
Then you can just read in your vector with read.table
, with sep=","
to put in a data.frame
Test:
string <- "[4] {PIVOTAL GEMFIRE} => {HIBERNATE} 0.005952381 1.0000000 168.000000 2 \r"
read.table(text=gsub("((\\S+)|(\\{[^{}]+\\}))\\s", "\\1,", trimws(gsub("[^[:alnum:].{}]+", " ", string))), sep=",")
# V1 V2 V3 V4 V5 V6 V7
#1 4 {PIVOTAL GEMFIRE} {HIBERNATE} 0.005952381 1 168 2