jdk/test/java/util/regex/RegExTest.java
changeset 4161 679d00486dc6
parent 2290 3a3bde061968
child 4820 e32f5d2ba062
equal deleted inserted replaced
4151:db7d59c0b0e6 4161:679d00486dc6
    30  * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
    30  * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
    31  * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
    31  * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
    32  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
    32  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
    33  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
    33  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
    34  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
    34  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
    35  * 6350801 6676425
    35  * 6350801 6676425 6878475
    36  */
    36  */
    37 
    37 
    38 import java.util.regex.*;
    38 import java.util.regex.*;
    39 import java.util.Random;
    39 import java.util.Random;
    40 import java.io.*;
    40 import java.io.*;
  3387         check(Pattern.compile("x+(?<gname>y+)z+"),
  3387         check(Pattern.compile("x+(?<gname>y+)z+"),
  3388               "xxxyyyzzz",
  3388               "xxxyyyzzz",
  3389               "gname",
  3389               "gname",
  3390               "yyy");
  3390               "yyy");
  3391 
  3391 
  3392         check(Pattern.compile("x+(?<8gname>y+)z+"),
  3392         check(Pattern.compile("x+(?<gname8>y+)z+"),
  3393               "xxxyyyzzz",
  3393               "xxxyyyzzz",
  3394               "8gname",
  3394               "gname8",
  3395               "yyy");
  3395               "yyy");
  3396 
  3396 
  3397         //backref
  3397         //backref
  3398         Pattern pattern = Pattern.compile("(a*)bc\\1");
  3398         Pattern pattern = Pattern.compile("(a*)bc\\1");
  3399         check(pattern, "zzzaabcazzz", true);  // found "abca"
  3399         check(pattern, "zzzaabcazzz", true);  // found "abca"
  3428               "yyy");
  3428               "yyy");
  3429 
  3429 
  3430         //replaceFirst/All
  3430         //replaceFirst/All
  3431         checkReplaceFirst("(?<gn>ab)(c*)",
  3431         checkReplaceFirst("(?<gn>ab)(c*)",
  3432                           "abccczzzabcczzzabccc",
  3432                           "abccczzzabcczzzabccc",
  3433                           "$<gn>",
  3433                           "${gn}",
  3434                           "abzzzabcczzzabccc");
  3434                           "abzzzabcczzzabccc");
  3435 
  3435 
  3436         checkReplaceAll("(?<gn>ab)(c*)",
  3436         checkReplaceAll("(?<gn>ab)(c*)",
  3437                         "abccczzzabcczzzabccc",
  3437                         "abccczzzabcczzzabccc",
  3438                         "$<gn>",
  3438                         "${gn}",
  3439                         "abzzzabzzzab");
  3439                         "abzzzabzzzab");
  3440 
  3440 
  3441 
  3441 
  3442         checkReplaceFirst("(?<gn>ab)(c*)",
  3442         checkReplaceFirst("(?<gn>ab)(c*)",
  3443                           "zzzabccczzzabcczzzabccczzz",
  3443                           "zzzabccczzzabcczzzabccczzz",
  3444                           "$<gn>",
  3444                           "${gn}",
  3445                           "zzzabzzzabcczzzabccczzz");
  3445                           "zzzabzzzabcczzzabccczzz");
  3446 
  3446 
  3447         checkReplaceAll("(?<gn>ab)(c*)",
  3447         checkReplaceAll("(?<gn>ab)(c*)",
  3448                         "zzzabccczzzabcczzzabccczzz",
  3448                         "zzzabccczzzabcczzzabccczzz",
  3449                         "$<gn>",
  3449                         "${gn}",
  3450                         "zzzabzzzabzzzabzzz");
  3450                         "zzzabzzzabzzzabzzz");
  3451 
  3451 
  3452         checkReplaceFirst("(?<gn1>ab)(?<gn2>c*)",
  3452         checkReplaceFirst("(?<gn1>ab)(?<gn2>c*)",
  3453                           "zzzabccczzzabcczzzabccczzz",
  3453                           "zzzabccczzzabcczzzabccczzz",
  3454                           "$<gn2>",
  3454                           "${gn2}",
  3455                           "zzzccczzzabcczzzabccczzz");
  3455                           "zzzccczzzabcczzzabccczzz");
  3456 
  3456 
  3457         checkReplaceAll("(?<gn1>ab)(?<gn2>c*)",
  3457         checkReplaceAll("(?<gn1>ab)(?<gn2>c*)",
  3458                         "zzzabccczzzabcczzzabccczzz",
  3458                         "zzzabccczzzabcczzzabccczzz",
  3459                         "$<gn2>",
  3459                         "${gn2}",
  3460                         "zzzccczzzcczzzccczzz");
  3460                         "zzzccczzzcczzzccczzz");
  3461 
  3461 
  3462         //toSupplementaries("(ab)(c*)"));
  3462         //toSupplementaries("(ab)(c*)"));
  3463         checkReplaceFirst("(?<gn1>" + toSupplementaries("ab") +
  3463         checkReplaceFirst("(?<gn1>" + toSupplementaries("ab") +
  3464                            ")(?<gn2>" + toSupplementaries("c") + "*)",
  3464                            ")(?<gn2>" + toSupplementaries("c") + "*)",
  3465                           toSupplementaries("abccczzzabcczzzabccc"),
  3465                           toSupplementaries("abccczzzabcczzzabccc"),
  3466                           "$<gn1>",
  3466                           "${gn1}",
  3467                           toSupplementaries("abzzzabcczzzabccc"));
  3467                           toSupplementaries("abzzzabcczzzabccc"));
  3468 
  3468 
  3469 
  3469 
  3470         checkReplaceAll("(?<gn1>" + toSupplementaries("ab") +
  3470         checkReplaceAll("(?<gn1>" + toSupplementaries("ab") +
  3471                         ")(?<gn2>" + toSupplementaries("c") + "*)",
  3471                         ")(?<gn2>" + toSupplementaries("c") + "*)",
  3472                         toSupplementaries("abccczzzabcczzzabccc"),
  3472                         toSupplementaries("abccczzzabcczzzabccc"),
  3473                         "$<gn1>",
  3473                         "${gn1}",
  3474                         toSupplementaries("abzzzabzzzab"));
  3474                         toSupplementaries("abzzzabzzzab"));
  3475 
  3475 
  3476         checkReplaceFirst("(?<gn1>" + toSupplementaries("ab") +
  3476         checkReplaceFirst("(?<gn1>" + toSupplementaries("ab") +
  3477                            ")(?<gn2>" + toSupplementaries("c") + "*)",
  3477                            ")(?<gn2>" + toSupplementaries("c") + "*)",
  3478                           toSupplementaries("abccczzzabcczzzabccc"),
  3478                           toSupplementaries("abccczzzabcczzzabccc"),
  3479                           "$<gn2>",
  3479                           "${gn2}",
  3480                           toSupplementaries("ccczzzabcczzzabccc"));
  3480                           toSupplementaries("ccczzzabcczzzabccc"));
  3481 
  3481 
  3482 
  3482 
  3483         checkReplaceAll("(?<gn1>" + toSupplementaries("ab") +
  3483         checkReplaceAll("(?<gn1>" + toSupplementaries("ab") +
  3484                         ")(?<gn2>" + toSupplementaries("c") + "*)",
  3484                         ")(?<gn2>" + toSupplementaries("c") + "*)",
  3485                         toSupplementaries("abccczzzabcczzzabccc"),
  3485                         toSupplementaries("abccczzzabcczzzabccc"),
  3486                         "$<gn2>",
  3486                         "${gn2}",
  3487                         toSupplementaries("ccczzzcczzzccc"));
  3487                         toSupplementaries("ccczzzcczzzccc"));
  3488 
  3488 
  3489         checkReplaceFirst("(?<dog>Dog)AndCat",
  3489         checkReplaceFirst("(?<dog>Dog)AndCat",
  3490                           "zzzDogAndCatzzzDogAndCatzzz",
  3490                           "zzzDogAndCatzzzDogAndCatzzz",
  3491                           "$<dog>",
  3491                           "${dog}",
  3492                           "zzzDogzzzDogAndCatzzz");
  3492                           "zzzDogzzzDogAndCatzzz");
  3493 
  3493 
  3494 
  3494 
  3495         checkReplaceAll("(?<dog>Dog)AndCat",
  3495         checkReplaceAll("(?<dog>Dog)AndCat",
  3496                           "zzzDogAndCatzzzDogAndCatzzz",
  3496                           "zzzDogAndCatzzzDogAndCatzzz",
  3497                           "$<dog>",
  3497                           "${dog}",
  3498                           "zzzDogzzzDogzzz");
  3498                           "zzzDogzzzDogzzz");
  3499 
  3499 
  3500         // backref in Matcher & String
  3500         // backref in Matcher & String
  3501         if (!"abcdefghij".replaceFirst("cd(?<gn>ef)gh", "$<gn>").equals("abefij") ||
  3501         if (!"abcdefghij".replaceFirst("cd(?<gn>ef)gh", "${gn}").equals("abefij") ||
  3502             !"abbbcbdbefgh".replaceAll("(?<gn>[a-e])b", "$<gn>").equals("abcdefgh"))
  3502             !"abbbcbdbefgh".replaceAll("(?<gn>[a-e])b", "${gn}").equals("abcdefgh"))
  3503             failCount++;
  3503             failCount++;
  3504 
  3504 
  3505         // negative
  3505         // negative
  3506         checkExpectedFail("(?<groupnamehasnoascii.in>abc)(def)");
  3506         checkExpectedFail("(?<groupnamehasnoascii.in>abc)(def)");
  3507         checkExpectedFail("(?<groupnamehasnoascii_in>abc)(def)");
  3507         checkExpectedFail("(?<groupnamehasnoascii_in>abc)(def)");
       
  3508         checkExpectedFail("(?<6groupnamestartswithdigit>abc)(def)");
  3508         checkExpectedFail("(?<gname>abc)(def)\\k<gnameX>");
  3509         checkExpectedFail("(?<gname>abc)(def)\\k<gnameX>");
  3509         checkExpectedFail("(?<gname>abc)(?<gname>def)\\k<gnameX>");
  3510         checkExpectedFail("(?<gname>abc)(?<gname>def)\\k<gnameX>");
  3510         checkExpectedFail(Pattern.compile("(?<gname>abc)(def)").matcher("abcdef"),
  3511         checkExpectedFail(Pattern.compile("(?<gname>abc)(def)").matcher("abcdef"),
  3511                           "gnameX");
  3512                           "gnameX");
  3512         checkExpectedFail(Pattern.compile("(?<gname>abc)(def)").matcher("abcdef"),
  3513         checkExpectedFail(Pattern.compile("(?<gname>abc)(def)").matcher("abcdef"),