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]);
}
gedithome/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