Search code examples
javaregexcharacter-class

How to represent an empty character class in Java regex


I want to create a character class that doesn't match any character. For now, I have been representing the pattern like this:

Pattern p = Pattern.compile("[a&&b]");

or even

Pattern p = Patterncompile("[^\\x{0}-\\x{10FFFF}]");

Is there a proper way to do this?

Note: there is no need to provide an alternative solution (negative lookahead, etc) that doesn't involve a character class. I'm just interested in knowing if there is a "right" way to do it.

Update: I'm implementing a feature which prints to the user the simplified version of what he inputted in the first place. Since I am simplifying character classes, my current version would just print [] for an input such as [a&&b]. If we parse [] again, it will throw an error, which is not what I want. Hence, that's why I wanted to know if there is a proper way to do it that does not alter the user input by showing him things like ^$


Solution

  • Altough that does not make much sense, you may use character class subtraction feature inside a character class to actually define a char and subtract it - then this class will not be able to match anything.

    Example:

    [a&&[^a]]
    

    Since the character class should only match a, and the a is substracted, this pattern will not match anything.