public class Matcher extends java.lang.Object implements MatchResult
Obtaining results
After the search succeded, i.e. if one of above methods returned true
one may obtain an information on the match:
Modifier and Type | Field and Description |
---|---|
static int |
ACCEPT_INCOMPLETE
Experimental option; if a text ends up before the end of a pattern,report a match.
|
static int |
ANCHOR_END
The same effect as "$" without REFlags.MULTILINE.
|
static int |
ANCHOR_LASTMATCH
The same effect as "\\G".
|
static int |
ANCHOR_START
The same effect as "^" without REFlags.MULTILINE.
|
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 |
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.
|
boolean |
getGroup(int n,
java.lang.StringBuffer sb) |
boolean |
getGroup(int n,
TextBuffer tb) |
boolean |
getGroup(java.lang.String name,
java.lang.StringBuffer sb) |
boolean |
getGroup(java.lang.String name,
TextBuffer tb) |
java.lang.String |
group() |
java.lang.String |
group(int n) |
java.lang.String |
group(java.lang.String name) |
int |
groupCount() |
java.lang.String[] |
groups() |
Vector |
groupv() |
boolean |
isCaptured() |
boolean |
isCaptured(int id) |
boolean |
isCaptured(java.lang.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(java.lang.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() |
java.lang.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:
|
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(CharStream in,
int len)
Supplies a text to search in/match with through a stream.
|
void |
setTarget(Matcher m,
int groupId)
This method allows to efficiently pass data between matchers.
|
void |
setTarget(java.lang.String text)
Supplies a text to search in/match with.
|
void |
setTarget(java.lang.String text,
int start,
int len)
Supplies a text to search in/match with, as a part of String.
|
void |
skip()
Sets the current search position just after the end of last match.
|
int |
start() |
int |
start(int id) |
java.lang.String |
suffix() |
java.lang.String |
target() |
char[] |
targetChars() |
int |
targetEnd() |
int |
targetStart() |
java.lang.String |
toString_d() |
java.lang.String |
toString() |
public static final int ANCHOR_START
find(int)
,
Constant Field Valuespublic static final int ANCHOR_LASTMATCH
find(int)
,
Constant Field Valuespublic static final int ANCHOR_END
find(int)
,
Constant Field Valuespublic static final int ACCEPT_INCOMPLETE
find(int)
,
Constant Field Valuespublic 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 suffixResets current search position to zero.
m
- - a matcher that is a source of datagroupId
- - which group to take data fromsetTarget(java.lang.String)
,
setTarget(java.lang.String,int,int)
,
setTarget(char[],int,int)
public void setTarget(java.lang.String text)
text
- - a datasetTarget(totalcross.util.regex.Matcher,int)
,
setTarget(java.lang.String,int,int)
,
setTarget(char[],int,int)
public void setTarget(java.lang.String text, int start, int len)
text
- - a data sourcestart
- - where the target startslen
- - how long is the targetsetTarget(totalcross.util.regex.Matcher,int)
,
setTarget(java.lang.String)
,
setTarget(char[],int,int)
public void setTarget(char[] text, int start, int len)
text
- - a data sourcestart
- - where the target startslen
- - how long is the targetsetTarget(totalcross.util.regex.Matcher,int)
,
setTarget(java.lang.String)
,
setTarget(java.lang.String,int,int)
public final 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 perfomanse 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(totalcross.util.regex.Matcher,int)
,
setTarget(java.lang.String)
,
setTarget(java.lang.String,int,int)
,
setTarget(char[],int,int)
public void setTarget(CharStream in, int len) throws IOException
in
- - a data stream;len
- - how much characters should be read; if len is -1, read the entire stream.IOException
setTarget(totalcross.util.regex.Matcher,int)
,
setTarget(java.lang.String)
,
setTarget(java.lang.String,int,int)
,
setTarget(char[],int,int)
public 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();
@Deprecated 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(java.lang.String s)
s
- the target string;public void setPosition(int pos)
public final boolean find()
true
if a match found.public final 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 final 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 nubmers occuring 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 final void skip()
public java.lang.String toString()
toString
in class java.lang.Object
public Pattern pattern()
pattern
in interface MatchResult
public java.lang.String target()
target
in interface MatchResult
public char[] targetChars()
targetChars
in interface MatchResult
public int targetStart()
targetStart
in interface MatchResult
public int targetEnd()
targetEnd
in interface MatchResult
public char charAt(int i)
charAt
in interface MatchResult
public char charAt(int i, int groupId)
charAt
in interface MatchResult
public final int length()
length
in interface MatchResult
public final int start()
start
in interface MatchResult
public final int end()
end
in interface MatchResult
public java.lang.String prefix()
prefix
in interface MatchResult
public java.lang.String suffix()
suffix
in interface MatchResult
public int groupCount()
groupCount
in interface MatchResult
public java.lang.String group()
group
in interface MatchResult
public java.lang.String group(int n)
group
in interface MatchResult
public java.lang.String group(java.lang.String name)
group
in interface MatchResult
public boolean getGroup(int n, TextBuffer tb)
getGroup
in interface MatchResult
public boolean getGroup(java.lang.String name, TextBuffer tb)
getGroup
in interface MatchResult
public boolean getGroup(int n, java.lang.StringBuffer sb)
getGroup
in interface MatchResult
public boolean getGroup(java.lang.String name, java.lang.StringBuffer sb)
getGroup
in interface MatchResult
public java.lang.String[] groups()
public Vector groupv()
public final boolean isCaptured()
isCaptured
in interface MatchResult
public final boolean isCaptured(int id)
isCaptured
in interface MatchResult
public final boolean isCaptured(java.lang.String groupName)
isCaptured
in interface MatchResult
public final int length(int id)
length
in interface MatchResult
public final int start(int id)
start
in interface MatchResult
public final int end(int id)
end
in interface MatchResult
public java.lang.String toString_d()