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: 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 default flags (0).
    static Pattern
    compile(String regex, int flags, Options options)
     
    static Pattern
    compile(String regex, Options options)
     
    boolean
    find(io.airlift.slice.Slice input)
     
    static boolean
    find(String regex, io.airlift.slice.Slice input)
    Matches a Slice against a regular expression (unanchored).
    static boolean
    find(String regex, io.airlift.slice.Slice input, Options options)
     
    int
    Returns the flags used in the constructor.
    int
    Returns the number of capturing groups in this matcher's pattern.
    matcher(io.airlift.slice.Slice input)
    Creates a new Matcher matching the pattern against the input.
    boolean
    matches(io.airlift.slice.Slice input)
     
    static boolean
    matches(String regex, io.airlift.slice.Slice input)
    Matches a Slice against a regular expression.
    static boolean
    matches(String regex, io.airlift.slice.Slice input, Options options)
     
    Returns the options used in the constructor.
    Returns the pattern used in the constructor.
    static String
    Returns a literal pattern string for the specified string.
    io.airlift.slice.Slice[]
    split(io.airlift.slice.Slice input)
    Splits input around instances of the regular expression.
    io.airlift.slice.Slice[]
    split(io.airlift.slice.Slice input, int limit)
    Splits input around instances of the regular expression.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, 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:
  • Method Details

    • flags

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

      public Options options()
      Returns the options 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, Options options)
    • compile

      public static Pattern compile(String regex, int flags)
      Creates and returns a new Pattern corresponding to compiling regex with the default flags (0).
      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
    • compile

      public static Pattern compile(String regex, int flags, Options options)
    • matches

      public static boolean matches(String regex, io.airlift.slice.Slice input)
      Matches a Slice 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, io.airlift.slice.Slice input, Options options)
    • matches

      public boolean matches(io.airlift.slice.Slice input)
    • find

      public static boolean find(String regex, io.airlift.slice.Slice input)
      Matches a Slice against a regular expression (unanchored).
      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
    • find

      public static boolean find(String regex, io.airlift.slice.Slice input, Options options)
    • find

      public boolean find(io.airlift.slice.Slice input)
    • matcher

      public Matcher matcher(io.airlift.slice.Slice input)
      Creates a new Matcher matching the pattern against the input.
      Parameters:
      input - the input Slice
    • split

      public io.airlift.slice.Slice[] split(io.airlift.slice.Slice input)
      Splits input around instances of the regular expression. It returns an array giving the Slices that occur before, between, and after instances of the regular expression. Empty Slices that would occur at the end of the array are omitted.
      Parameters:
      input - the input Slice to be split
      Returns:
      the split Slices
    • split

      public io.airlift.slice.Slice[] split(io.airlift.slice.Slice input, int limit)
      Splits input around instances of the regular expression. It returns an array giving the Slices 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 Slices that would occur at the end of the array are omitted. If limit > 0, at most limit Slices are returned. The final Slice contains the remainder of the input, possibly including additional matches of the pattern.

      Parameters:
      input - the input Slice to be split
      limit - the limit
      Returns:
      the split Slices
    • 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 enture pattern and is excluded from this count.
      Returns:
      the number of capturing groups in this pattern