Class Pattern

java.lang.Object
com.google.re2j.Pattern
All Implemented Interfaces:
Serializable

public final class Pattern extends Object implements 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:
  • Field Summary

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

    Modifier and Type
    Method
    Description
    static Pattern
    compile(String regex)
    Creates and returns a new Pattern corresponding to compiling regex with the default flags (0).
    static Pattern
    compile(String regex, int flags)
    Creates and returns a new Pattern corresponding to compiling regex with the given flags.
    boolean
     
    int
    Returns the flags used in the constructor.
    int
    Returns the number of capturing groups in this matcher's pattern.
    int
     
    matcher(byte[] input)
     
    Creates a new Matcher matching the pattern against the input.
    boolean
    matches(byte[] input)
     
    boolean
    matches(String input)
     
    static boolean
    matches(String regex, byte[] input)
     
    static boolean
    matches(String regex, CharSequence input)
    Matches a string against a regular expression.
    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.
    Returns the pattern used in the constructor.
    static String
    Returns a literal pattern string for the specified string.
    void
    Releases memory used by internal caches associated with this pattern.
    split(String input)
    Splits input around instances of the regular expression.
    split(String input, int limit)
    Splits input around instances of the regular expression.
     

    Methods inherited from class java.lang.Object

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

    • CASE_INSENSITIVE

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

      public static final int DOTALL
      Flag: dot (.) matches all characters, including newline.
      See Also:
    • 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:
    • DISABLE_UNICODE_GROUPS

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

      public static final int LONGEST_MATCH
      Flag: matches longest possible string.
      See Also:
  • Method Details

    • 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 String pattern()
      Returns the pattern used in the constructor.
    • compile

      public static Pattern compile(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(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
      IllegalArgumentException - if an unknown flag is given
    • matches

      public static boolean matches(String regex, 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(String regex, byte[] input)
    • matches

      public boolean matches(String input)
    • matches

      public boolean matches(byte[] input)
    • matcher

      public Matcher matcher(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 String[] split(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 String[] split(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 String quote(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 String toString()
      Overrides:
      toString in class 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 Map<String,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(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object