I have a string to be tokenized based on the , character. Problem here is the string is like this
-123 abc, 234 def (2,3,4), -456 zyx (4,5,6) and xyz (6,5,4), 789 final!
After tokenizing output should look like…
-123 abc
234 def (2,3,4)
-456 zyx (4,5,6) and xyz (6,5,4)
789 final!
How to write the regular expression for this? TIA.
Tokenizer will not do, you need a parser. Regexps are not particularly good at counting, and that is what you need to decide on where to end a parenthesized block with commas inside.
A simple Recursive Descent Parser should work in your case. You may want to get fancy and try ANTLR. It is a great and powerful tool, but it is probably an overkill for simple expression such as the ones in your example.