Class Pattern

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Pattern
    extends java.lang.Object
    implements java.io.Serializable
    A compiled representation of an RE2 regular expression, mimicking the java.util.regex.Pattern API.

    The matching functions take String arguments instead of the more general Java CharSequence since the latter doesn't provide UTF-16 decoding.

    See the package-level documentation for an overview of how to use this API.

    Author:
    rsc@google.com (Russ Cox)
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int CASE_INSENSITIVE
      Flag: case insensitive matching.
      static int DISABLE_UNICODE_GROUPS
      Flag: Unicode groups (e.g.
      static int DOTALL
      Flag: dot (.) matches all characters, including newline.
      static int LONGEST_MATCH
      Flag: matches longest possible string.
      static int MULTILINE
      Flag: multiline matching: ^ and $ match at beginning and end of line, not just beginning and end of input.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Pattern compile​(java.lang.String regex)
      Creates and returns a new Pattern corresponding to compiling regex with the default flags (0).
      static Pattern compile​(java.lang.String regex, int flags)
      Creates and returns a new Pattern corresponding to compiling regex with the given flags.
      boolean equals​(java.lang.Object o)  
      int flags()
      Returns the flags used in the constructor.
      int groupCount()
      Returns the number of capturing groups in this matcher's pattern.
      int hashCode()  
      Matcher matcher​(byte[] input)  
      Matcher matcher​(java.lang.CharSequence input)
      Creates a new Matcher matching the pattern against the input.
      boolean matches​(byte[] input)  
      boolean matches​(java.lang.String input)  
      static boolean matches​(java.lang.String regex, byte[] input)  
      static boolean matches​(java.lang.String regex, java.lang.CharSequence input)
      Matches a string against a regular expression.
      java.util.Map<java.lang.String,​java.lang.Integer> namedGroups()
      Return a map of the capturing groups in this matcher's pattern, where key is the name and value is the index of the group in the pattern.
      java.lang.String pattern()
      Returns the pattern used in the constructor.
      static java.lang.String quote​(java.lang.String s)
      Returns a literal pattern string for the specified string.
      void reset()
      Releases memory used by internal caches associated with this pattern.
      java.lang.String[] split​(java.lang.String input)
      Splits input around instances of the regular expression.
      java.lang.String[] split​(java.lang.String input, int limit)
      Splits input around instances of the regular expression.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • CASE_INSENSITIVE

        public static final int CASE_INSENSITIVE
        Flag: case insensitive matching.
        See Also:
        Constant Field Values
      • DOTALL

        public static final int DOTALL
        Flag: dot (.) matches all characters, including newline.
        See Also:
        Constant Field Values
      • MULTILINE

        public static final int MULTILINE
        Flag: multiline matching: ^ and $ match at beginning and end of line, not just beginning and end of input.
        See Also:
        Constant Field Values
      • DISABLE_UNICODE_GROUPS

        public static final int DISABLE_UNICODE_GROUPS
        Flag: Unicode groups (e.g. \p\ Greek\ ) will be syntax errors.
        See Also:
        Constant Field Values
      • LONGEST_MATCH

        public static final int LONGEST_MATCH
        Flag: matches longest possible string.
        See Also:
        Constant Field Values
    • Method Detail

      • reset

        public void reset()
        Releases memory used by internal caches associated with this pattern. Does not change the observable behaviour. Useful for tests that detect memory leaks via allocation tracking.
      • flags

        public int flags()
        Returns the flags used in the constructor.
      • pattern

        public java.lang.String pattern()
        Returns the pattern used in the constructor.
      • compile

        public static Pattern compile​(java.lang.String regex)
        Creates and returns a new Pattern corresponding to compiling regex with the default flags (0).
        Parameters:
        regex - the regular expression
        Throws:
        PatternSyntaxException - if the pattern is malformed
      • compile

        public static Pattern compile​(java.lang.String regex,
                                      int flags)
        Creates and returns a new Pattern corresponding to compiling regex with the given flags.
        Parameters:
        regex - the regular expression
        flags - bitwise OR of the flag constants CASE_INSENSITIVE, DOTALL, and MULTILINE
        Throws:
        PatternSyntaxException - if the regular expression is malformed
        java.lang.IllegalArgumentException - if an unknown flag is given
      • matches

        public static boolean matches​(java.lang.String regex,
                                      java.lang.CharSequence input)
        Matches a string against a regular expression.
        Parameters:
        regex - the regular expression
        input - the input
        Returns:
        true if the regular expression matches the entire input
        Throws:
        PatternSyntaxException - if the regular expression is malformed
      • matches

        public static boolean matches​(java.lang.String regex,
                                      byte[] input)
      • matches

        public boolean matches​(java.lang.String input)
      • matches

        public boolean matches​(byte[] input)
      • matcher

        public Matcher matcher​(java.lang.CharSequence input)
        Creates a new Matcher matching the pattern against the input.
        Parameters:
        input - the input string
      • matcher

        public Matcher matcher​(byte[] input)
      • split

        public java.lang.String[] split​(java.lang.String input)
        Splits input around instances of the regular expression. It returns an array giving the strings that occur before, between, and after instances of the regular expression. Empty strings that would occur at the end of the array are omitted.
        Parameters:
        input - the input string to be split
        Returns:
        the split strings
      • split

        public java.lang.String[] split​(java.lang.String input,
                                        int limit)
        Splits input around instances of the regular expression. It returns an array giving the strings that occur before, between, and after instances of the regular expression.

        If limit <= 0, there is no limit on the size of the returned array. If limit == 0, empty strings that would occur at the end of the array are omitted. If limit > 0, at most limit strings are returned. The final string contains the remainder of the input, possibly including additional matches of the pattern.

        Parameters:
        input - the input string to be split
        limit - the limit
        Returns:
        the split strings
      • quote

        public static java.lang.String quote​(java.lang.String s)
        Returns a literal pattern string for the specified string.

        This method produces a string that can be used to create a Pattern that would match the string s as if it were a literal pattern.

        Metacharacters or escape sequences in the input sequence will be given no special meaning.
        Parameters:
        s - The string to be literalized
        Returns:
        A literal string replacement
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • groupCount

        public int groupCount()
        Returns the number of capturing groups in this matcher's pattern. Group zero denotes the entire pattern and is excluded from this count.
        Returns:
        the number of capturing groups in this pattern
      • namedGroups

        public java.util.Map<java.lang.String,​java.lang.Integer> namedGroups()
        Return a map of the capturing groups in this matcher's pattern, where key is the name and value is the index of the group in the pattern.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object