I created a regex pattern that works perfect, but I can’t get it working in Java:
(\\"|[^" ])+|"(\\"|[^"])*"
applied to
robocopy "C:\test" "C:\test2" /R:0 /MIR /NP
gives (as it should)
[0] => robocopy
[1] => "C:\test"
[2] => "C:\test2"
[3] => /R:0
[4] => /MIR
[5] => /NP
in group 0 according to http://myregextester.com/index.php
Now, how do I get those 6 values in Java?
I tried
Pattern p = Pattern.compile(" (\\\"|[^\" ])+ | \"(\\\"|[^\"])*\" ");
Matcher m = p.matcher(command);
System.out.println(m.matches()); // returns false
but the pattern doesn’t even match anything at all?
Update
The original perl regex was:
(\\"|[^" ])+|"(\\"|[^"])*"
Since the regexp string is first processed by the compiler before making it to the regexp processor, you need to double every backslahs in the expression, and add additional slashes for every doublequote.