1
String line = "This order was placed for QT3000! OK?";
    String pattern = "(.*)(\\d+)(.*)";

    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);
    if (m.find()) {
      System.out.println("Found value: " + m.group(1));
      System.out.println("Found value: " + m.group(2));
      System.out.println("Found value: " + m.group(3));
    }

出力は

Found value: This order was placed for QT300
Found value: 0
Found value: ! OK?

私は出力を期待していましたが

Found value: This order was placed for QT3000! OK?
Found value: 3000
Found value: This order was placed for QT3000! OK?

私の期待される出力の理由は

If pattern is  "(.*)"   output for m.group(1) is "This order was placed for QT3000! OK?"
If pattern is  "(\\d+)" output for m.group(1) is "3000"

パターンをいつ言及するかわかりません"(.*)(\\d+)(.*)"; 期待される出力が得られないのはなぜですか?

4

2 に答える 2