Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4499535

regex character classes: caret after '\[' negates rest of class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta3
    • generic
    • generic
    • Verified



      Name: nt126004 Date: 09/04/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)


      The regex parser is too simplistic in determining whether a
      caret in a character class should be treated as a negation
      operator. The first run of the sample program below shows a
      properly-working character class: the caret matches a literal
      caret because it's not the first character in the class. The
      second regex, with the caret right after the escaped left-
      bracket, should work the same, but it doesn't match the carets
      in the target string any more. In the third run, we can see
      that the caret is actually negating the rest of the class, in
      this case cancelling out the first at-sign.



      //====================== sample code ===========================

      import java.util.regex.*;

      public class PatternTest
      {
        public static void main(String[] argv)
        {
          Pattern p1 = Pattern.compile(argv[0]);
          Matcher m1 = p1.matcher("@@@@[[[[^^^^");
          System.out.println(m1.find() ? "found: " + m1.group()
                                       : "not found");
        }
      }

      //======================== output =============================

      $ java PatternTest '[\[@^]+'
      found: @@@@[[[[^^^^


      $ java PatternTest '[@\[^]+'
      found: @@@@[[[[


      $ java PatternTest '[@\[^@]+'
      found: [[[[
      (Review ID: 131194)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: