public final class TPattern extends Object implements Serializable
Pattern
s are specified in terms of regular expressions and compiled
using an instance of this class. They are then used in conjunction with a
TMatcher
to perform the actual search.
A typical use case looks like this:
Pattern p = Pattern.compile("Hello, A[a-z]*!"); Matcher m = p.matcher("Hello, Android!"); boolean b1 = m.matches(); // true m.setInput("Hello, Robot!"); boolean b2 = m.matches(); // falseThe above code could also be written in a more compact fashion, though this variant is less efficient, since
Pattern
and Matcher
objects
are created on the fly instead of being reused. fashion:
boolean b1 = Pattern.matches("Hello, A[a-z]*!", "Hello, Android!"); // true boolean b2 = Pattern.matches("Hello, A[a-z]*!", "Hello, Robot!"); // false
TMatcher
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
CANON_EQ
This constant specifies that a character in a
Pattern and a
character in the input string only match if they are canonically
equivalent. |
static int |
CASE_INSENSITIVE
This constant specifies that a
Pattern is matched
case-insensitively. |
static int |
COMMENTS
This constant specifies that a
Pattern may contain whitespace or
comments. |
static int |
DOTALL
This constant specifies that the '.' meta character matches arbitrary
characters, including line endings, which is normally not the case.
|
static int |
LITERAL
This constant specifies that the whole
Pattern is to be taken
literally, that is, all meta characters lose their meanings. |
static int |
MULTILINE
This constant specifies that the meta characters '^' and '$' match only
the beginning and end end of an input line, respectively.
|
static int |
UNICODE_CASE
This constant specifies that a
Pattern is matched
case-insensitively with regard to all Unicode characters. |
static int |
UNIX_LINES
This constant specifies that a pattern matches Unix line endings ('\n')
only against the '
|
Modifier and Type | Method and Description |
---|---|
static TPattern |
compile(String pattern)
Compiles a regular expression, creating a new Pattern instance in the
process.
|
static TPattern |
compile(String pattern,
int flags)
Compiles a regular expression, creating a new
Pattern instance in
the process. |
int |
flags()
Returns the flags that have been set for this
Pattern . |
TMatcher |
matcher(CharSequence input)
Returns a
TMatcher for the Pattern and a given input. |
static boolean |
matches(String regex,
CharSequence input)
Tries to match a given regular expression against a given input.
|
String |
pattern()
Returns the regular expression that was compiled into this
Pattern . |
static String |
quote(String s)
Quotes a given string using "\Q" and "\E", so that all other
meta-characters lose their special meaning.
|
String[] |
split(CharSequence input)
Splits a given input around occurrences of a regular expression.
|
String[] |
split(CharSequence inputSeq,
int limit)
Splits the given input sequence around occurrences of the
Pattern
. |
String |
toString() |
public static final int UNIX_LINES
public static final int CASE_INSENSITIVE
Pattern
is matched
case-insensitively. That is, the patterns "a+" and "A+" would both match
the string "aAaAaA".public static final int COMMENTS
Pattern
may contain whitespace or
comments. Otherwise comments and whitespace are taken as literal
characters.public static final int MULTILINE
public static final int LITERAL
Pattern
is to be taken
literally, that is, all meta characters lose their meanings.public static final int DOTALL
public static final int UNICODE_CASE
Pattern
is matched
case-insensitively with regard to all Unicode characters. It is used in
conjunction with the CASE_INSENSITIVE
constant to extend its
meaning to all Unicode characters.public static final int CANON_EQ
Pattern
and a
character in the input string only match if they are canonically
equivalent.public TMatcher matcher(CharSequence input)
TMatcher
for the Pattern
and a given input. The
Matcher
can be used to match the Pattern
against the
whole input, find occurrences of the Pattern
in the input, or
replace parts of the input.input
- the input to process.Matcher
.public String[] split(CharSequence inputSeq, int limit)
Pattern
. The function first determines all occurrences of the Pattern
inside the input sequence. It then builds an array of the
"remaining" strings before, in-between, and after these
occurrences. An additional parameter determines the maximal number of
entries in the resulting array and the handling of trailing empty
strings.inputSeq
- the input sequence.limit
- Determines the maximal number of entries in the resulting
array.
Pattern
+1. All
entries are included.
Pattern
+1. Empty
strings at the end of the array are not included.
public String[] split(CharSequence input)
split(java.lang.CharSequence, int)
with a limit of 0.input
- the input sequence.public String pattern()
Pattern
.public int flags()
Pattern
.CANON_EQ
,
CASE_INSENSITIVE
,
COMMENTS
,
DOTALL
,
LITERAL
,
MULTILINE
,
UNICODE_CASE
,
UNIX_LINES
public static TPattern compile(String pattern, int flags) throws TPatternSyntaxException
Pattern
instance in
the process. Allows to set some flags that modify the behavior of the
Pattern
.pattern
- the regular expression.flags
- the flags to set. Basically, any combination of the constants
defined in this class is valid.Pattern
instance.TPatternSyntaxException
- if the regular expression is syntactically incorrect.CANON_EQ
,
CASE_INSENSITIVE
,
COMMENTS
,
DOTALL
,
LITERAL
,
MULTILINE
,
UNICODE_CASE
,
UNIX_LINES
public static TPattern compile(String pattern)
compile(String, int)
with a flags
value of zero.pattern
- the regular expression.Pattern
instance.TPatternSyntaxException
- if the regular expression is syntactically incorrect.public static boolean matches(String regex, CharSequence input)
Pattern
, builds a TMatcher
for it, and
then does the match. If the same regular expression is used for multiple
operations, it is recommended to compile it into a Pattern
explicitly and request a reusable Matcher
.regex
- the regular expression.input
- the input to process.Pattern
matches the input.compile(java.lang.String, int)
,
TMatcher.matches()
Copyright © 2019. All rights reserved.