Java - 正则表达式 - “”之间的捕获组

时间:2013-03-17 11:13:25

标签: java regex

你知道这里有什么问题吗?

    Pattern pathsPatter = Pattern.compile("\"([^\"]+)\"");
    Matcher pathsMatcher = pathsPatter.matcher(commandAndParameters[1]);

我想捕捉“”之间的群组。例如,如果字符串是

    mv "C:\Users\" "D:\"
匹配器应该捕获:

    C:\Users\
    D:\

2 个答案:

答案 0 :(得分:1)

尝试这种模式:

    String data = "mv \"C:\\Users\\\" \"D:\\\"";

    Pattern pattern = Pattern.compile("\"(.+?)\"");
    Matcher matcher = pattern.matcher(data);
    System.out.println("Started");
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }

答案 1 :(得分:0)

在这里工作正常:

Pattern pathsPattern = Pattern.compile("\"([^\"]+)\"");
Matcher pathsMatcher = pathsPattern.matcher("mv \"C:\\Users\\\" \"D:\\\"");
pathsMatcher.find();
System.out.println("found " + pathsMatcher.group(1)); // prints: found C:\Users\
pathsMatcher.find();
System.out.println("found " + pathsMatcher.group(1)); // prints: found D:\