Package com.google.re2j
Class Pattern
java.lang.Object
com.google.re2j.Pattern
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionstatic 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 TypeMethodDescriptionstatic Pattern
Creates and returns a newPattern
corresponding to compilingregex
with the default flags (0).static Pattern
Creates and returns a newPattern
corresponding to compilingregex
with the givenflags
.boolean
int
flags()
Returns the flags used in the constructor.int
Returns the number of capturing groups in this matcher's pattern.int
hashCode()
matcher
(byte[] input) matcher
(CharSequence input) Creates a newMatcher
matching the pattern against the input.boolean
matches
(byte[] input) boolean
static boolean
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.pattern()
Returns the pattern used in the constructor.static String
Returns a literal pattern string for the specified string.void
reset()
Releases memory used by internal caches associated with this pattern.String[]
Splits input around instances of the regular expression.String[]
Splits input around instances of the regular expression.toString()
-
Field Details
-
CASE_INSENSITIVE
public static final int CASE_INSENSITIVEFlag: case insensitive matching.- See Also:
-
DOTALL
public static final int DOTALLFlag: dot (.
) matches all characters, including newline.- See Also:
-
MULTILINE
public static final int MULTILINEFlag: 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_GROUPSFlag: Unicode groups (e.g.\p\ Greek\
) will be syntax errors.- See Also:
-
LONGEST_MATCH
public static final int LONGEST_MATCHFlag: 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
Returns the pattern used in the constructor. -
compile
Creates and returns a newPattern
corresponding to compilingregex
with the default flags (0).- Parameters:
regex
- the regular expression- Throws:
PatternSyntaxException
- if the pattern is malformed
-
compile
Creates and returns a newPattern
corresponding to compilingregex
with the givenflags
.- Parameters:
regex
- the regular expressionflags
- bitwise OR of the flag constantsCASE_INSENSITIVE
,DOTALL
, andMULTILINE
- Throws:
PatternSyntaxException
- if the regular expression is malformedIllegalArgumentException
- if an unknown flag is given
-
matches
Matches a string against a regular expression.- Parameters:
regex
- the regular expressioninput
- the input- Returns:
- true if the regular expression matches the entire input
- Throws:
PatternSyntaxException
- if the regular expression is malformed
-
matches
-
matches
-
matches
public boolean matches(byte[] input) -
matcher
Creates a newMatcher
matching the pattern against the input.- Parameters:
input
- the input string
-
matcher
-
split
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
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. Iflimit == 0
, empty strings that would occur at the end of the array are omitted. Iflimit > 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 splitlimit
- the limit- Returns:
- the split strings
-
quote
Returns a literal pattern string for the specified string.This method produces a string that can be used to create a
Metacharacters or escape sequences in the input sequence will be given no special meaning.Pattern
that would match the strings
as if it were a literal pattern.- Parameters:
s
- The string to be literalized- Returns:
- A literal string replacement
-
toString
-
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
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
-
hashCode
public int hashCode()
-