|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--jregex.Matcher
Matcher instance is an automaton that actually performs matching. It provides the following methods:
Obtaining results
After the search succeded, i.e. if one of above methods returned true
one may obtain an information on the match:
Field Summary | |
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. |
Fields inherited from interface jregex.MatchResult |
MATCH, PREFIX, SUFFIX, TARGET |
Method Summary | |
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(int n)
|
java.lang.String |
group(java.lang.String name)
|
int |
groupCount()
|
java.lang.String[] |
groups()
|
java.util.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: Matcher m=new Pattern("\\w+").matcher("abc"); while(m.proceed(0)){ System.out.println(m.group(0)); } Output: abc ab a bc b c For 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 3 Note that using find() method we would find '123' only. |
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)
For wise people only. |
void |
setTarget(Matcher m,
int groupId)
This method allows to efficiently pass data between matchers. |
void |
setTarget(java.io.Reader in,
int len)
Supplies a text to search in/match with through a stream. |
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()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int ANCHOR_START
find(int)
public static final int ANCHOR_LASTMATCH
find(int)
public static final int ANCHOR_END
find(int)
public static final int ACCEPT_INCOMPLETE
find(int)
Method Detail |
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 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)
,
setTarget(java.io.Reader,int)
public void setTarget(java.lang.String text)
text
- - a datasetTarget(jregex.Matcher,int)
,
setTarget(java.lang.String,int,int)
,
setTarget(char[],int,int)
,
setTarget(java.io.Reader,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(jregex.Matcher,int)
,
setTarget(java.lang.String)
,
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(jregex.Matcher,int)
,
setTarget(java.lang.String)
,
setTarget(java.lang.String,int,int)
,
setTarget(java.io.Reader,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 being 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.- See Also:
setTarget(jregex.Matcher,int)
,
setTarget(java.lang.String)
,
setTarget(java.lang.String,int,int)
,
setTarget(char[],int,int)
,
setTarget(java.io.Reader,int)
public void setTarget(java.io.Reader in, int len) throws java.io.IOException
in
- - a data stream;len
- - how much characters should be read; if len is -1, read the entire stream.setTarget(jregex.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();
- Returns:
- true if the entire target matches the beginning of the pattern
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();
- Returns:
- whether a current target matches the whole pattern.
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(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 java.util.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()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |