public final class Pattern extends java.lang.Object implements java.lang.Comparable<Pattern>, java.io.Serializable
Patterns are created indirectly by calling
compile(java.lang.String)
and passing in the
regular expression text. If the regular expression is
successfully compiled, then a Pattern
instance is
returned. Use the matches(java.lang.CharSequence)
method to test if a character sequence matches the regular
expression.
Pattern instance are immutable and safe for concurrent thread usage.
Construct | Matches |
---|---|
Characters | |
x | The character x |
\\ | The backslash character |
\0n | The character with octal value 0n (0 ≤ n ≤ 7) |
\0nn | The character with octal value 0nn (0 ≤ n ≤ 7) |
\0mnn | The character with octal value 0mnn (0 ≤ m ≤ 3, 0 ≤ n ≤ 7) |
\xhh | The character with hexadecimal value 0xhh |
\uhhhh | The character with hexadecimal value 0xhhhh |
\t | The tab character ('\u0009') |
\n | The newline (line feed) character ('\u000A') |
\r | The carriage-return character ('\u000D') |
\f | The form-feed character ('\u000C') |
\a | The alert (bell) character ('\u0007') |
\e | The escape character ('\u001B') |
Character classes | |
[abc] | a, b, or c (simple class) |
[^abc] | Any character except a, b, or c (negation) |
[a-zA-Z] | a through z or A through Z, inclusive (range) |
Predefined character classes | |
. | Any character. |
\d | A digit: [0-9] |
\D | A non-digit: [^0-9] |
\s | A whitespace character: [ \t\n\0x0B\f\r] |
\S | A non-whitespace character: [^\s] |
\w | A word character: [a-zA-Z_0-9] |
\W | A non-word character: [^\w] |
Greedy quantifiers | |
X? | X, once or not at all. |
X* | X, zero or more times. |
X+ | X, one or more times. |
X{n} | X, exactly n times. |
X{n,} | X, at least n times. |
X{, n} | X, zero to at most n times. |
X{n, m} | X, at least n times but no more than m times where n ≤ m. |
This regular expression class is specifically designed to
efficiently search
TernarySearchTree
keys. Hence the
reason for developing a separate regular expression package
from java.util.regex
. If you
need to perform regular expression pattern matching over
generic character sequences, then use the
java.util.regex.Pattern
class which supports
more extensive regular expression constructs.
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Pattern pattern)
Returns an integer value < zero if this pattern is
less than the argument; zero if this pattern is equal
to the argument; > zero if this pattern is greater
than the argument.
|
static Pattern |
compile(java.lang.String regex)
Returns the eBus regular expression pattern compiled from
the given string.
|
Component[] |
components()
Returns the pattern's components.
|
boolean |
equals(java.lang.Object object)
Returns
true if this object and the argument
object are both regular expression patterns
with the same underlying pattern string and
false otherwise. |
int |
hashCode()
Returns a hosh code based on the regular expression
pattern string.
|
boolean |
matches(java.lang.CharSequence input)
Returns
true if this regular expression
pattern accepts input and false otherwise. |
int |
maximumSize()
Returns a matching string's maximum possible size.
|
int |
minimumSize()
Returns a matching string's minimum possible size.
|
java.lang.String |
pattern()
Returns the original regular expression pattern string.
|
java.lang.String |
toString()
Returns this pattern's textual representation.
|
public int compareTo(Pattern pattern)
compareTo
in interface java.lang.Comparable<Pattern>
pattern
- Compare agains this patternpublic java.lang.String pattern()
public Component[] components()
public int minimumSize()
public int maximumSize()
-1
means there is no limit.public boolean matches(java.lang.CharSequence input)
true
if this regular expression
pattern accepts input
and false
otherwise.input
- match this character sequence against this
regular expression pattern.true
if this regular expression
pattern accepts input
and false
otherwise.public boolean equals(java.lang.Object object)
true
if this object and the argument
object
are both regular expression patterns
with the same underlying pattern string and
false
otherwise.equals
in class java.lang.Object
object
- test equality with this object.true
if this object and the argument
object
are both regular expression patterns
with the same underlying pattern string and
false
otherwise.public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public static Pattern compile(java.lang.String regex)
regex
- compile this string into an eBus pattern.java.lang.IllegalArgumentException
- if regex
is null
.java.util.regex.PatternSyntaxException
- if regex
's syntax is invalid.Copyright © 2001 - 2024. Charles W. Rapp. All rights reserved.