0

This is my string

"/opt/jboss-eap/bin/jboss-cli.sh --connect --controller=localhost:9990 -c command=\"deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force\""

I want to split the above string as follows

/opt/jboss-eap/bin/jboss-cli.sh
--connect
--controller=localhost:9990
-c
command="deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force"

I had tried with following code

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
Matcher regexMatcher = regex.matcher("/opt/jboss-eap/bin/jboss-cli.sh --connect --controller=localhost:9990 -c command=\"deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force\"");
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
} 
out.println("matchList="+matchList);

But the output is

matchList=[/opt/jboss-eap/bin/jboss-cli.sh, --connect, --controller=localhost:9990, -c, command=, "deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force"]

/opt/jboss-eap/bin/jboss-cli.sh
--connect
--controller=localhost:9990
-c
command=
"deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force"

Its for java 'ProcessBuilder'

Edit

I had using another method from this link Tokenizing a String but ignoring delimiters within quotes

String str = "/opt/jboss-eap/bin/jboss-cli.sh --connect --controller=localhost:9990 -c command=\"deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force\"";
String regex = "\"([^\"]*)\"|(\\S+)";
List<String> list = new ArrayList<String>();
Matcher m = Pattern.compile(regex).matcher(str);
while (m.find()) 
{
    if (m.group(1) != null)
    {
        list.add(m.group(1));
    } 
    else 
    {
        list.add(m.group(2));
    }
}
out.println(list);

Output is

/opt/jboss-eap/bin/jboss-cli.sh
--connect
--controller=localhost:9990
-c
command="deploy
/app/jboss-eap-7.1/standalone/updates/sample.war
--force"
Pamba
  • 776
  • 1
  • 16
  • 29
  • This method https://stackoverflow.com/questions/3366281/tokenizing-a-string-but-ignoring-delimiters-within-quotes give the output [/opt/jboss-eap/bin/jboss-cli.sh, --connect, --controller=localhost:9990, -c, command="deploy, /app/jboss-eap-7.1/standalone/updates/sample.war, --force"] – Pamba Apr 02 '19 at 06:16
  • 1
    Yes, I don't this it is an exact duplicate. But use this regex `Pattern.compile("(\\S+\"([^\"]*)\")|\"([^\"]*)\"|(\\S+)");`. This should work for you. – HariUserX Apr 02 '19 at 06:26
  • Thank you very much 'HariUserX'. Its working as expected! – Pamba Apr 02 '19 at 06:32
  • 1
    In case you need a detailed explanation, I have added an answer to the post https://stackoverflow.com/questions/3366281/tokenizing-a-string-but-ignoring-delimiters-within-quotes/55468731#55468731 – HariUserX Apr 02 '19 at 07:02

1 Answers1

0

Just use String split#method with multiple delimiters.

String string = "/opt/jboss-eap/bin/jboss-cli.sh --connect --controller=localhost:9990 -c command=\"deploy /app/jboss-eap-7.1/standalone/updates/sample.war --force\";
String[] parts = string.split("((-c)|(\\--))");
String part1 = parts[0]; 
String part2 = parts[1]; 

And so on with as many parts as you have. This way you will keep also the delimiter inside string parts.