public class Matcher extends Object implements MatchResult
Obtaining results
After the search succeeded, i.e. if one of above methods returned true
one may obtain an information on the match:
MATCH, PREFIX, SUFFIX, TARGET| Modifier and Type | Method and Description |
|---|---|
char |
charAt(int i) |
char |
charAt(int i,
int groupId) |
int |
end() |
int |
end(int id) |
boolean |
equals(Object o) |
boolean |
find()
Searches through a target for a matching substring, starting from just after the end of last match.
|
boolean |
find(int anchors)
Searches through a target for a matching substring, starting from just after the end of last match.
|
MatchIterator |
findAll()
The same as findAll(int), but with default behaviour;
|
MatchIterator |
findAll(int options)
Returns an iterator over the matches found by subsequently calling find(options), the search starts from the zero position.
|
void |
flush()
Resets the internal state.
|
boolean |
getGroup(int n,
StringBuilder sb) |
boolean |
getGroup(int n,
TextBuffer tb) |
boolean |
getGroup(String name,
StringBuilder sb) |
boolean |
getGroup(String name,
TextBuffer tb) |
String |
getString(int start,
int end) |
String |
group(int n) |
String |
group(String name) |
int |
groupCount() |
String[] |
groups() |
ArrayList<String> |
groupv() |
int |
hashCode() |
boolean |
isCaptured() |
boolean |
isCaptured(int id) |
boolean |
isCaptured(String groupName) |
boolean |
isStart()
Deprecated.
Replaced by isPrefix()
|
int |
length() |
int |
length(int id) |
boolean |
matches()
Tells whether a current target matches the whole pattern.
|
boolean |
matches(String s)
Just a combination of setTarget(String) and matches().
|
boolean |
matchesPrefix()
Tells whether the entire target matches the beginning of the pattern.
|
Pattern |
pattern() |
String |
prefix() |
boolean |
proceed()
Continues to search from where the last search left off.
|
boolean |
proceed(int options)
Continues to search from where the last search left off using specified options:
|
String |
replaceAll(String replacement) |
String |
replaceAmount(String replacement,
int amount) |
String |
replaceFirst(String replacement) |
boolean |
search(int anchors) |
void |
setAll(Reader in) |
void |
setPosition(int pos)
Allows to set a position the subsequent find()/find(int) will start from.
|
void |
setTarget(char[] text,
int start,
int len)
Supplies a text to search in/match with, as a part of char array.
|
void |
setTarget(char[] text,
int start,
int len,
boolean shared)
To be used with much care.
|
void |
setTarget(CharSequence text)
Supplies a text to search in/match with.
|
void |
setTarget(CharSequence text,
int start,
int len)
Supplies a text to search in/match with, as a part of String.
|
void |
setTarget(Matcher m,
int groupId)
This method allows to efficiently pass data between matchers.
|
void |
setTarget(Reader in,
int len)
Supplies a text to search in/match with through a stream.
|
void |
skip()
Sets the current search position just after the end of last match.
|
int |
start() |
int |
start(int id) |
String |
suffix() |
String |
target() |
char[] |
targetChars() |
int |
targetEnd() |
int |
targetStart() |
String |
toString() |
public final void setTarget(Matcher m, int groupId)
Matcher m=new Pattern("\\w+").matcher(myString);
if(m.find())m.setTarget(m,m.SUFFIX); //forget all that is not a suffix
Resets current search position to zero.m - - a matcher that is a source of datagroupId - - which group to take data fromsetTarget(java.lang.CharSequence),
setTarget(java.lang.CharSequence, int, int),
setTarget(char[], int, int),
setTarget(java.io.Reader, int)public void setTarget(CharSequence text)
text - - a datasetTarget(regexodus.Matcher, int),
setTarget(java.lang.CharSequence, int, int),
setTarget(char[], int, int),
setTarget(java.io.Reader, int)public void setTarget(CharSequence text, int start, int len)
text - - a data sourcestart - - where the target startslen - - how long is the targetsetTarget(regexodus.Matcher, int),
setTarget(java.lang.CharSequence),
setTarget(char[], int, int),
setTarget(java.io.Reader, int)public void setTarget(char[] text, int start, int len)
text - - a data sourcestart - - where the target startslen - - how long is the targetsetTarget(regexodus.Matcher, int),
setTarget(java.lang.CharSequence),
setTarget(java.lang.CharSequence, int, int),
setTarget(java.io.Reader, int)public void setTarget(char[] text, int start, int len, boolean shared)
shared=false:myMatcher.setTarget(myCharArray,x,y,false); //we declare that array contents is NEITHER shared NOR will be used later, so may modifications on it are permittedthen we should expect the array contents to be changed on subsequent setTarget(..) operations. Such method may yield some increase in perfomance in the case of multiple setTarget() calls. Resets current search position to zero.
text - - a data sourcestart - - where the target startslen - - how long is the targetshared - - if true: data are shared or used later, don't modify it; if false: possible modifications of the text on subsequent setTarget() calls are perceived and allowed.setTarget(regexodus.Matcher, int),
setTarget(java.lang.CharSequence),
setTarget(java.lang.CharSequence, int, int),
setTarget(char[], int, int),
setTarget(java.io.Reader, int)public void setTarget(Reader in, int len) throws IOException
in - - a data stream;len - - how much characters should be read; if len is -1, read the entire stream.IOExceptionsetTarget(regexodus.Matcher, int),
setTarget(java.lang.CharSequence),
setTarget(java.lang.CharSequence, int, int),
setTarget(char[], int, int)public void setAll(Reader in) throws IOException
IOExceptionpublic final boolean matchesPrefix()
true:
Pattern p=new Pattern("abcd");
p.matcher("").matchesPrefix();
p.matcher("a").matchesPrefix();
p.matcher("ab").matchesPrefix();
p.matcher("abc").matchesPrefix();
p.matcher("abcd").matchesPrefix();
and the following yield false:
p.matcher("b").isPrefix();
p.matcher("abcdef").isPrefix();
p.matcher("x").isPrefix();
public final boolean isStart()
public final boolean matches()
true:
Pattern p=new Pattern("\\w+");
p.matcher("a").matches();
p.matcher("ab").matches();
p.matcher("abc").matches();
and the following yields the false:
p.matcher("abc def").matches();
p.matcher("bcd ").matches();
p.matcher(" bcd").matches();
p.matcher("#xyz#").matches();
public final boolean matches(String s)
s - the target string;public void setPosition(int pos)
public final boolean find()
true if a match found.public boolean find(int anchors)
anchors - a zero or a combination(bitwise OR) of ANCHOR_START,ANCHOR_END,ANCHOR_LASTMATCH,ACCEPT_INCOMPLETEtrue if a match found.public MatchIterator findAll()
public MatchIterator findAll(int options)
public final boolean proceed()
proceed(int)public boolean proceed(int options)
Matcher m=new Pattern("\\w+").matcher("abc");
while(m.proceed(0)){
System.out.println(m.group(0));
}
Output:abc ab a bc b cFor example, let's find all odd numbers occurring in a text:
Matcher m=new Pattern("\\d+").matcher("123");
while(m.proceed(0)){
String match=m.group(0);
if(isOdd(Integer.parseInt(match))) System.out.println(match);
}
static boolean isOdd(int i){
return (i&1)>0;
}
This outputs:123 1 23 3Note that using
find() method we would find '123' only.options - search options, some of ANCHOR_START|ANCHOR_END|ANCHOR_LASTMATCH|ACCEPT_INCOMPLETE; zero value(default) stands for usual search for substring.public void skip()
public void flush()
public Pattern pattern()
pattern in interface MatchResultpublic String target()
target in interface MatchResultpublic char[] targetChars()
targetChars in interface MatchResultpublic int targetStart()
targetStart in interface MatchResultpublic int targetEnd()
targetEnd in interface MatchResultpublic char charAt(int i)
charAt in interface MatchResultpublic char charAt(int i, int groupId)
charAt in interface MatchResultpublic final int length()
length in interface MatchResultpublic final int start()
start in interface MatchResultpublic final int end()
end in interface MatchResultpublic String prefix()
prefix in interface MatchResultpublic String suffix()
suffix in interface MatchResultpublic int groupCount()
groupCount in interface MatchResultpublic String group(int n)
group in interface MatchResultpublic String group(String name)
group in interface MatchResultpublic boolean getGroup(int n, TextBuffer tb)
getGroup in interface MatchResultpublic boolean getGroup(String name, TextBuffer tb)
getGroup in interface MatchResultpublic boolean getGroup(int n, StringBuilder sb)
getGroup in interface MatchResultpublic boolean getGroup(String name, StringBuilder sb)
getGroup in interface MatchResultpublic final boolean isCaptured()
isCaptured in interface MatchResultpublic final boolean isCaptured(int id)
isCaptured in interface MatchResultpublic final boolean isCaptured(String groupName)
isCaptured in interface MatchResultpublic final int length(int id)
length in interface MatchResultpublic final int start(int id)
start in interface MatchResultpublic final int end(int id)
end in interface MatchResultpublic boolean search(int anchors)
public String replaceFirst(String replacement)
public String replaceAmount(String replacement, int amount)
public String replaceAll(String replacement)
Copyright © 2016. All rights reserved.