Hax Hax - 17 days ago 5
Java Question

Print a string with parsing specifiers

I have a string like

"(+ x (division (- x 1)))"
by which can be parsed using
split("\\s*[\\(\\),]+\\s*")


Output:

x
sumup
x
1


I need that output according to the paranthesis and spaces(which are the specifiers) in order. I mean I expect that output:

(
+
x
(
division
(
-
x
1
)
)
)


How can be it overcome?

Answer

This seems to provide your desired output:

s.replaceAll("([\\(\\)\\ ])","\n$1\n").replaceAll("\\s*\n+","\n")
Comments