请帮助编写一个Pattern,仅在命令及其参数之间的空格上拆分命令行:
我的代码:
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
家/蚂蚁/文档/ 1.txt的
并且有必要这样做:
gedit
/ home / ant / Documents / Txt Books / 1.txt
/home/ant/Documents/1.txt
答案 0 :(得分:1)
您还可以尝试使用split()方法分割STRING
例如:
请帮助编写一个Pattern,仅在命令及其参数之间的空格上拆分命令行:
String commands = "gedit /home/ant/Documents/Txt Books/1.txt /home/ant/Documents/1.
String[] commandsToArray=commands.split("\\s+");//this wil ignore multiples whitespaces
答案 1 :(得分:0)
您需要使用此正则表达式进行拆分
\\s+(?=[^\\w\\s])
在你的正则表达式中,[^\\\s\\\w]
会在空格后吃掉第一个字符。结果......
使用只会检查模式但不会吃它的lookahead
......