Class Pattern

  • All Implemented Interfaces:
    Serializable, Comparable<Pattern>

    public final class Pattern
    extends Object
    implements Comparable<Pattern>, Serializable
    A compiled regular expression representation.

    Patterns are created indirectly by calling compile(java.lang.String) and passing in the regular expression text. If the regular expression is successfully compiled, then a Pattern instance is returned. Use the matches(java.lang.CharSequence) method to test if a character sequence matches the regular expression.

    Pattern instance are immutable and safe for concurrent thread usage.

    Summary of regular-expression constructs

    Construct Matches
     
    Characters
    x The character x
    \\ The backslash character
    \0n The character with octal value 0n (0 ≤ n ≤ 7)
    \0nn The character with octal value 0nn (0 ≤ n ≤ 7)
    \0mnn The character with octal value 0mnn (0 ≤ m ≤ 3, 0 ≤ n ≤ 7)
    \xhh The character with hexadecimal value 0xhh
    \uhhhh The character with hexadecimal value 0xhhhh
    \t The tab character ('\u0009')
    \n The newline (line feed) character ('\u000A')
    \r The carriage-return character ('\u000D')
    \f The form-feed character ('\u000C')
    \a The alert (bell) character ('\u0007')
    \e The escape character ('\u001B')
     
    Character classes
    [abc] a, b, or c (simple class)
    [^abc] Any character except a, b, or c (negation)
    [a-zA-Z] a through z or A through Z, inclusive (range)
     
    Predefined character classes
    . Any character.
    \d A digit: [0-9]
    \D A non-digit: [^0-9]
    \s A whitespace character: [ \t\n\0x0B\f\r]
    \S A non-whitespace character: [^\s]
    \w A word character: [a-zA-Z_0-9]
    \W A non-word character: [^\w]
     
    Greedy quantifiers
    X? X, once or not at all.
    X* X, zero or more times.
    X+ X, one or more times.
    X{n} X, exactly n times.
    X{n,} X, at least n times.
    X{, n} X, zero to at most n times.
    X{n, m} X, at least n times but no more than m times where nm.

    This regular expression class is specifically designed to efficiently search TernarySearchTree keys. Hence the reason for developing a separate regular expression package from java.util.regex. If you need to perform regular expression pattern matching over generic character sequences, then use the java.util.regex.Pattern class which supports more extensive regular expression constructs.

    Author:
    Charles Rapp
    See Also:
    TernarySearchTree.entrySet(Pattern), TernarySearchTree.entrySet(Pattern, int), TernarySearchTree.keySet(Pattern), TernarySearchTree.keySet(Pattern, int), TernarySearchTree.values(Pattern), TernarySearchTree.values(Pattern, int), Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Pattern pattern)
      Returns an integer value < zero if this pattern is less than the argument; zero if this pattern is equal to the argument; > zero if this pattern is greater than the argument.
      static Pattern compile​(String regex)
      Returns the eBus regular expression pattern compiled from the given string.
      Component[] components()
      Returns the pattern's components.
      boolean equals​(Object object)
      Returns true if this object and the argument object are both regular expression patterns with the same underlying pattern string and false otherwise.
      int hashCode()
      Returns a hosh code based on the regular expression pattern string.
      boolean matches​(CharSequence input)
      Returns true if this regular expression pattern accepts input and false otherwise.
      int maximumSize()
      Returns a matching string's maximum possible size.
      int minimumSize()
      Returns a matching string's minimum possible size.
      String pattern()
      Returns the original regular expression pattern string.
      String toString()
      Returns this pattern's textual representation.
    • Method Detail

      • compareTo

        public int compareTo​(Pattern pattern)
        Returns an integer value < zero if this pattern is less than the argument; zero if this pattern is equal to the argument; > zero if this pattern is greater than the argument.
        Specified by:
        compareTo in interface Comparable<Pattern>
        Parameters:
        pattern - Compare agains this pattern
        Returns:
        an integer value < zero if this pattern is less than the argument; zero if this pattern is equal to the argument; > zero if this pattern is greater than the argument.
      • pattern

        public String pattern()
        Returns the original regular expression pattern string.
        Returns:
        the original regular expression pattern string.
      • components

        public Component[] components()
        Returns the pattern's components.
        Returns:
        the pattern's components.
      • minimumSize

        public int minimumSize()
        Returns a matching string's minimum possible size.
        Returns:
        a matching string's minimum possible size.
      • maximumSize

        public int maximumSize()
        Returns a matching string's maximum possible size. -1 means there is no limit.
        Returns:
        a matching string's maximum possible size.
      • matches

        public boolean matches​(CharSequence input)
        Returns true if this regular expression pattern accepts input and false otherwise.
        Parameters:
        input - match this character sequence against this regular expression pattern.
        Returns:
        true if this regular expression pattern accepts input and false otherwise.
      • equals

        public boolean equals​(Object object)
        Returns true if this object and the argument object are both regular expression patterns with the same underlying pattern string and false otherwise.
        Overrides:
        equals in class Object
        Parameters:
        object - test equality with this object.
        Returns:
        true if this object and the argument object are both regular expression patterns with the same underlying pattern string and false otherwise.
      • toString

        public String toString()
        Returns this pattern's textual representation.
        Overrides:
        toString in class Object
        Returns:
        this pattern's textual representation.
      • hashCode

        public int hashCode()
        Returns a hosh code based on the regular expression pattern string.
        Overrides:
        hashCode in class Object
        Returns:
        a hosh code based on the regular expression pattern string.
      • compile

        public static Pattern compile​(String regex)
        Returns the eBus regular expression pattern compiled from the given string.
        Parameters:
        regex - compile this string into an eBus pattern.
        Returns:
        the eBus regular expression pattern compiled from the given string.
        Throws:
        IllegalArgumentException - if regex is null.
        PatternSyntaxException - if regex's syntax is invalid.