0

Do you know what is wrong here?

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

I want to capture the group between " ". For example, if the string is

    mv "C:\Users\" "D:\"

the matcher should capture:

    C:\Users\
    D:\
John Smith
  • 1,276
  • 4
  • 17
  • 35

2 Answers2

1

Try out this pattern :

    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));
    }
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

Works fine here:

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:\
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255