|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.regex.Matcher
public final class Matcher
Instance of a regular expression applied to a char sequence.
Method Summary | |
---|---|
Matcher |
appendReplacement(StringBuffer sb,
String replacement)
|
StringBuffer |
appendTail(StringBuffer sb)
|
int |
end()
Returns the index just after the last matched character. |
int |
end(int group)
Returns the index just after the last matched character of the given sub-match group. |
boolean |
find()
|
boolean |
find(int start)
|
String |
group()
Returns the substring of the input which was matched. |
String |
group(int group)
Returns the substring of the input which was matched by the given sub-match group. |
int |
groupCount()
Returns the number of sub-match groups in the matching pattern. |
boolean |
hasAnchoringBounds()
Returns true if the matcher will honour the use of the anchoring bounds: ^ , \A , \Z ,
\z and $ . |
boolean |
hasTransparentBounds()
Returns true if the bounds of the region marked by regionStart() and regionEnd() are
transparent. |
boolean |
hitEnd()
|
boolean |
lookingAt()
|
boolean |
matches()
Attempts to match the entire input sequence against the pattern. |
Pattern |
pattern()
Returns the Pattern that is interpreted by this Matcher |
Matcher |
region(int start,
int end)
Defines the region of the input on which to match. |
int |
regionEnd()
The end of the region on which to perform matches (exclusive). |
int |
regionStart()
The start of the region on which to perform matches (inclusive). |
String |
replaceAll(String replacement)
|
String |
replaceFirst(String replacement)
|
Matcher |
reset()
Resets the internal state of the matcher, including resetting the region to its default state of encompassing the whole input. |
Matcher |
reset(CharSequence input)
Resets the internal state of the matcher, including resetting the region to its default state of encompassing the whole input. |
int |
start()
Returns the index of the first character of the match. |
int |
start(int group)
Returns the index of the first character of the given sub-match group. |
MatchResult |
toMatchResult()
Returns a read-only snapshot of the current state of the Matcher as a MatchResult . |
String |
toString()
Convert this Object to a human-readable String. |
Matcher |
useAnchoringBounds(boolean useAnchors)
Enables or disables the use of the anchoring bounds: ^ , \A , \Z , \z and
$ . |
Matcher |
useTransparentBounds(boolean transparent)
Sets the transparency of the bounds of the region marked by regionStart() and regionEnd() . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public Matcher appendReplacement(StringBuffer sb, String replacement) throws IllegalStateException
sb
- The target string bufferreplacement
- The replacement string
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failed
IndexOutOfBoundsException
- If the replacement string refers
to a capturing group that does not exist in the patternpublic StringBuffer appendTail(StringBuffer sb)
sb
- The target string bufferpublic int end() throws IllegalStateException
MatchResult
end
in interface MatchResult
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failedpublic int end(int group) throws IllegalStateException
MatchResult
end
in interface MatchResult
group
- The index of a capturing group in this matcher's pattern
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failed
IndexOutOfBoundsException
- If the replacement string refers
to a capturing group that does not exist in the patternpublic boolean find()
public boolean find(int start)
start
- The index to start the new pattern matching
IndexOutOfBoundsException
- If the replacement string refers
to a capturing group that does not exist in the patternpublic String group()
MatchResult
group
in interface MatchResult
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failedpublic String group(int group) throws IllegalStateException
MatchResult
group
in interface MatchResult
group
- The index of a capturing group in this matcher's pattern
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failed
IndexOutOfBoundsException
- If the replacement string refers
to a capturing group that does not exist in the patternpublic String replaceFirst(String replacement)
replacement
- The replacement stringpublic String replaceAll(String replacement)
replacement
- The replacement stringpublic int groupCount()
MatchResult
groupCount
in interface MatchResult
public boolean lookingAt()
public boolean matches()
start()
,
end()
,
group()
public Pattern pattern()
public Matcher reset()
hasTransparentBounds()
and hasAnchoringBounds()
are unaffected.
regionStart()
,
regionEnd()
,
hasTransparentBounds()
,
hasAnchoringBounds()
public Matcher reset(CharSequence input)
hasTransparentBounds()
and hasAnchoringBounds()
are unaffected.
input
- The new input character sequence.
regionStart()
,
regionEnd()
,
hasTransparentBounds()
,
hasAnchoringBounds()
public int start() throws IllegalStateException
MatchResult
start
in interface MatchResult
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failedpublic int start(int group) throws IllegalStateException
MatchResult
start
in interface MatchResult
group
- The index of a capturing group in this matcher's pattern
IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failed
IndexOutOfBoundsException
- If the replacement string refers
to a capturing group that does not exist in the patternpublic boolean hitEnd()
public String toString()
Object
System.out.println()
and such.
It is typical, but not required, to ensure that this method
never completes abruptly with a RuntimeException
.
This method will be called when performing string
concatenation with this object. If the result is
null
, string concatenation will instead
use "null"
.
The default implementation returns
getClass().getName() + "@" +
Integer.toHexString(hashCode())
.
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public Matcher region(int start, int end)
Defines the region of the input on which to match.
By default, the Matcher
attempts to match
the whole string (from 0 to the length of the input),
but a region between start
(inclusive) and
end
(exclusive) on which to match may instead
be defined using this method.
The behaviour of region matching is further affected
by the use of transparent or opaque bounds (see
useTransparentBounds(boolean)
) and whether or not
anchors (^
and $
) are in use
(see useAnchoringBounds(boolean)
). With transparent
bounds, the matcher is aware of input outside the bounds
set by this method, whereas, with opaque bounds (the default)
only the input within the bounds is used. The use of
anchors are affected by this setting; with transparent
bounds, anchors will match the beginning of the real input,
while with opaque bounds they match the beginning of the
region. useAnchoringBounds(boolean)
can be used
to turn on or off the matching of anchors.
start
- the start of the region (inclusive).end
- the end of the region (exclusive).
IndexOutOfBoundsException
- if either start
or
end
are less than zero,
if either start
or
end
are greater than the
length of the input, or if
start
is greater than
end
.regionStart()
,
regionEnd()
,
hasTransparentBounds()
,
useTransparentBounds(boolean)
,
hasAnchoringBounds()
,
useAnchoringBounds(boolean)
public int regionStart()
#see #regionEnd()
public int regionEnd()
region(int,int)
,
regionStart()
public boolean hasTransparentBounds()
regionStart()
and regionEnd()
are
transparent. When these bounds are transparent, the
matching process can look beyond them to perform
lookahead, lookbehind and boundary matching operations.
By default, the bounds are opaque.
useTransparentBounds(boolean)
,
region(int,int)
,
regionStart()
,
regionEnd()
public Matcher useTransparentBounds(boolean transparent)
regionStart()
and regionEnd()
.
A value of true
makes the bounds transparent,
so the matcher can see beyond them to perform lookahead,
lookbehind and boundary matching operations. A value
of false
(the default) makes the bounds opaque,
restricting the match to the input region denoted
by regionStart()
and regionEnd()
.
transparent
- true if the bounds should be transparent.
hasTransparentBounds()
,
region(int,int)
,
regionStart()
,
regionEnd()
public boolean hasAnchoringBounds()
^
, \A
, \Z
,
\z
and $
. By default, the anchors
are used. Note that the effect of the anchors is
also affected by hasTransparentBounds()
.
useAnchoringBounds(boolean)
,
hasTransparentBounds()
public Matcher useAnchoringBounds(boolean useAnchors)
^
, \A
, \Z
, \z
and
$
. By default, their use is enabled. When
disabled, the matcher will not attempt to match
the anchors.
useAnchors
- true if anchoring bounds should be used.
hasAnchoringBounds()
public MatchResult toMatchResult()
Matcher
as a MatchResult
. Any
subsequent changes to this instance are not reflected
in the returned MatchResult
.
MatchResult
instance representing the
current state of the Matcher
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |