Vipul Paralikar
Vipul Paralikar

Reputation: 1568

Tokenize string in java using regular expression

I have input String in following format:

"xyz PQ (All) (foo \"(abc\" def) test \"elasticSearch\" (test \"pqr\" stu) "

I want to tokenize it such that the string should be split by space excluding double quotes and parentheses, so it should give below result

xyz
PQ
All
foo "(abc" def
test
"elasticSearch"
test "pqr" stu

Any help would be appreciated.

Upvotes: 2

Views: 61

Answers (2)

Vipul Paralikar
Vipul Paralikar

Reputation: 1568

following regex solves the purpose:

(\\([^)]+\\))|([^ \"]*\"[^\"]*\")|([^ ]+)

Upvotes: 1

Zereges
Zereges

Reputation: 5209

(\([^)]+\))|([^ ]+)

Explanations and usage here.

Upvotes: 1

Related Questions