JDK-8228606 : Negation on nested character classes does not work
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2019-07-19
  • Updated: 2019-07-25
  • Resolved: 2019-07-25
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Win 10 / Java 1.8.0_181

A DESCRIPTION OF THE PROBLEM :
Negation on nested character classes does not work
Pattern.compile("[^[a]]").matcher("a").matches(); 
provides true, but should provide false

(same for more complicated expressions) 
if this should not be supported I would expect a proper exception for an invalid syntax

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        // shows version 1.8.0_181
        System.out.println(System.getProperty("java.version"));

        // OK: runs through => matches a => fine
        assert Pattern.compile("[[a]]").matcher("a").matches();

        // OK: does not match a 
        assert ! Pattern.compile("[^a]").matcher("a").matches();

        // NOT OK: does still match a = > assertion fails
        assert ! Pattern.compile("[^[a]]").matcher("a").matches();


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
expected

[^[a]] should have same semantic as [a]

=>
Pattern.compile("[^[a]]").matcher("a").matches(); 
should result in false, not true
ACTUAL -
Pattern.compile("[^[a]]").matcher("a").matches();  

=> false

---------- BEGIN SOURCE ----------
s. above
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
avoid nested character classes and negation

FREQUENCY : always



Comments
This has been fixed in JDK 9 onwards with JDK-6609854.
25-07-2019