Class CharacterSet

java.lang.Object
com.google.common.labs.parse.CharacterSet
All Implemented Interfaces:
CharPredicate

public final class CharacterSet extends Object implements CharPredicate
Represents a set of characters specified by a regex-like character set string.

For example charsIn("[a-zA-Z-_]") is a shorthand of CharPredicate.range('a', 'z').orRange('A', 'Z').or('-').or('_').

You can also use '^' to get negative character set like: charsIn("[^a-zA-Z]"), which is any non-alphabet character.

Note that it's different from CharPredicate.anyOf(string), which treats the string as a list of literal characters, not a regex-like character set.

It's strongly recommended to install the mug-errorprone plugin (v9.4+) in your compiler's and IDE's annotationProcessorPaths so that you can get instant feedback against incorrect character set syntax.

Implementation Note: regex isn't used during parsing. The character set string is translated to a plain CharPredicate at construction time.

Since:
9.4
  • Method Details

    • charsIn

      public static CharacterSet charsIn(String characterSet)
      Returns a CharacterSet instance compiled from the given characterSet specifier.
      Parameters:
      characterSet - A regex-like character set string (e.g. "[a-zA-Z0-9-_]"), but disallows backslash so doesn't support escaping. If your character set includes special characters like literal backslash or right bracket, use CharPredicate instead.
      Throws:
      IllegalArgumentException - if characterSet includes backslash or the right bracket (except the outmost pairs of []).
    • test

      public boolean test(char ch)
      Returns true if this set contains the character ch.
      Specified by:
      test in interface CharPredicate
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object