Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA编写一个模式,将命令行拆分到间隙_Java_Split_Command Line Arguments_Design Patterns - Fatal编程技术网

JAVA编写一个模式,将命令行拆分到间隙

JAVA编写一个模式,将命令行拆分到间隙,java,split,command-line-arguments,design-patterns,Java,Split,Command Line Arguments,Design Patterns,请帮助编写一个模式,仅在命令及其参数之间的空格上拆分命令行: String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1. String[] commandsToArray=commands.split("\\s+");//this wil ignore multiples whitespaces 我的代码: String commands = "gedit /home/ant/Docum

请帮助编写一个模式,仅在命令及其参数之间的空格上拆分命令行:

String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.
String[] commandsToArray=commands.split("\\s+");//this wil ignore multiples whitespaces
我的代码:

String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.txt";

String[] arrCommands = Pattern.compile("\\\s[^\\\s\\\w]").split(commands);

for (int i = 0; i < arrCommands.length; i++) {
     System.out.println(arrCommands[i]);
}
String commands=“gedit/home/ant/Documents/Txt Books/1.Txt/home/ant/Documents/1.Txt”;
String[]arrcomands=Pattern.compile(\\\s[^\\\\s\\\\w]).split(命令);
for(int i=0;i
程序给出以下结果:

String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.txt";

String[] arrCommands = Pattern.compile("\\\s[^\\\s\\\w]").split(commands);

for (int i = 0; i < arrCommands.length; i++) {
     System.out.println(arrCommands[i]);
}
gedit
home/ant/Documents/Txt Books/1.Txt
home/ant/Documents/1.txt

而且有必要这样做:

String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.txt";

String[] arrCommands = Pattern.compile("\\\s[^\\\s\\\w]").split(commands);

for (int i = 0; i < arrCommands.length; i++) {
     System.out.println(arrCommands[i]);
}
gedit
/home/ant/Documents/Txt Books/1.Txt
/home/ant/Documents/1.txt


您需要使用此正则表达式进行拆分

\\s+(?=[^\\w\\s])

在您的正则表达式中,
[^\\\\s\\\w]
将首先吃掉空格后的字符。。因此结果


使用
lookahead
,它只会检查模式,但不会吃掉模式…

您需要使用此正则表达式进行拆分

\\s+(?=[^\\w\\s])

在您的正则表达式中,
[^\\\\s\\\w]
将首先吃掉空格后的字符。。因此结果


使用
lookahead
,它只会检查模式,但不会吃掉模式…

您还可以尝试使用SPLIT()方法拆分字符串

例如:

请帮助编写一个模式,仅在命令及其参数之间的空格上拆分命令行:

String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.
String[] commandsToArray=commands.split("\\s+");//this wil ignore multiples whitespaces

您还可以尝试使用SPLIT()方法拆分字符串

例如:

请帮助编写一个模式,仅在命令及其参数之间的空格上拆分命令行:

String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.
String[] commandsToArray=commands.split("\\s+");//this wil ignore multiples whitespaces

结果gedit home/ant/Documents/Txt Books/1.Txt home/ant/Documents/1.Txt需要:gedit/home/ant/Documents/Txt Books/1.Txt结果gedit home/ant/Documents/Txt Books/1.Txt home/ant/Documents/1.Txt需要:gedit/home/ant/Documents/Txt Books/1.Txt/home/ant/Documents/1.Txt