I am trying to split commands using semicolon in Java. The semicolons within the single or double quotes should be ignored and commands are separated based on semicolons not within the single or double quotes.
Example command: echo 'he ; llo' ; echo 'hello;;'
I tried the following code but the command doesn't get split correctly:
String[] tokens = cmdline.split(";(?=(?:[^\"\']*\"\'[^\"\']*\"\')*[^\"\']*$)", -1);
Current incorrect splitting:
echo 'he ; llo' ; echo 'hello;;'
Expected to split into:
1) echo 'he ; llo'
2) echo 'hello;;'