字符串: abc 123 1 "d e f"
要拆分成:
abc
123
1
d e f如果使用split(空格),则双引号里面的也被拆分了。
现在想找个正则表达式,能忽略双引号里面的空格。

解决方案 »

  1.   

    先索引出引号的位置,按索引位置截取引号里面的内容。然后剩下的以空格截取。代码就不给你写了 对你来说应该是 easy 的
      

  2.   

    String str ="abc 123 1 'd e f'";
    String[] ss = str.split("\\s(^'\\s*')");
      

  3.   

    String s = "abc 123 1 \"d e f\"".replaceAll("\"", "") ;
    System.out.println(s);
      

  4.   

    String str ="abc 123 1 \"d e f\"";
    String[] ss = str.split("\\s(^\"\\s*\")");
      

  5.   


    Pattern p = Pattern.compile("\".+\"|\\S+\\s");
    |符号前后不能换顺序。
      

  6.   


    你写的是这个么?
    String str ="abc 123 1 \"d e f\"";
    String[] ss = str.split("\\s(^\"\\s*\")");我测试的结果是:完全没效果啊
      ss数组的长度是1
      

  7.   


    你坑,能再坑?String str ="abc 123 1 \"d e f\"";
    String[] ss = str.split("\\s(^\"\\s*\")");
    for(String s:ss){
    System.out.println(s);
    }
      

  8.   

    String str = "abc 123 1 \"d e fs\" \"d e f\" \"d e f\" \"d e f\" \"d e f\"";
    String[] array=str.split("\\s\"");
    int length=array==null?0:array.length;
    for(int i=0;i<length;i++){
    if(array[i].endsWith("\"")){
    System.out.println(array[i].replaceAll("\"", ""));
    }else{
    String[] temp=array[i].split(" ");
    int tempLength=temp==null?0:temp.length;
    for(int j=0;j<tempLength;j++){
    System.out.println(temp[j]);
    }
    }
    }
      

  9.   


    自己分析还不如用现成的库呢  opencsv好用省事。 public static void main(String[] args) throws IOException {
    String str = "abc 123 1 \"d e f\"";
    CSVReader reader = new CSVReader(new CharArrayReader(str.toCharArray()), ' ');
        String [] nextLine;
        while ((nextLine = reader.readNext()) != null) {
         for (String s : nextLine) {
         System.out.println(s);
         }
        }
    }
     opencsv
      

  10.   

    大致想了想,还是稍微尝试用了下正则:        String str = "abc 123 1 \"d e fs\" \"X Y Z\" \"a\" \"X X\" W \"Y Y\"";
            Scanner sc = new Scanner(str);
            ArrayList<String> lst = new ArrayList<String>();
            while (sc.hasNext()) {
                lst.add(sc.findInLine("\"[^\"]+\"|\\S+").replaceAll("\"", ""));
            }
            String[] result = lst.toArray(new String[0]);
            System.out.println(lst);也即输入字符串为:
      abc 123 1 "d e fs" "X Y Z" "a" "X X" W "Y Y"
    分解结果是:
      [abc, 123, 1, d e fs, X Y Z, a, X X, W, Y Y]
      

  11.   

    谢谢大伙了。其实,我应该把问题说全面一点,我就是为了处理命令行参数的问题。比如:console -a 123 -b 456 -c "q w e" -d    
    (-a,-b,-c带参数,-d不带参数,-c的参数有空格,使用双引号括起来)我想把 -c 的参数 "q w e" 取出来。刚才看到ldh911同学用Scanner的findInLine方法,其实我这一行字符串本就是通过Scanner读取进来的。
      

  12.   


    并不影响你再用一个新的Scanner去扫描你所读取出来的这行字符串。只不过是说,既然你存在 参数名和参数值 关系,其实也不用转成数组了,直接List就可以了,反正你还得再遍历一次来解释各参数关系以及让参数发生效用。
      

  13.   


    对。
    我就是再想,可不可以只用一次Scannaer,在scanner读取的时候就顺便给解析了,避免创建两次对象呢。
      

  14.   


    有个库Args4j。 专门分析命令行参数的。
    http://sunxboy.iteye.com/blog/697708
    如果你们参数也挺复杂,可以推荐使用库。
      

  15.   


    也可以,无非是你先用Scanner把前面那个“console”读取掉,然后面就用 findInLine就行了。
      

  16.   

    一个简单的状态机,遇空格action,遇"就暂时抑制空格action。
      

  17.   


    哦,好的,我去查一查。
    目前用的是commons-cli-1.2来处理命令参数的,不过首先得把命令的数组args[]弄出来。
    我去看看args4j是不是更方便。