Package totalcross.util.regex
Class Matcher
- java.lang.Object
-
- totalcross.util.regex.Matcher
-
- All Implemented Interfaces:
MatchResult
public class Matcher extends java.lang.Object implements MatchResult
Matcher instance is an automaton that actually performs matching. It provides the following methods:- searching for a matching substrings : matcher.find() or matcher.findAll();
- testing whether a text matches a whole pattern : matcher.matches();
- testing whether the text matches the beginning of a pattern : matcher.matchesPrefix();
- searching with custom options : matcher.find(int options)
Obtaining results
After the search succeded, i.e. if one of above methods returnedtrueone may obtain an information on the match:- may check whether some group is captured : matcher.isCaptured(int);
- may obtain start and end positions of the match and its length : matcher.start(int),matcher.end(int),matcher.length(int);
- may obtain match contents as String : matcher.group(int).
The same way can be obtained the match prefix and suffix information. The appropriate methods are grouped in MatchResult interface, which the Matcher class implements.
Matcher objects are not thread-safe, so only one thread may use a matcher instance at a time. Note, that Pattern objects are thread-safe(the same instanse may be shared between multiple threads), and the typical tactics in multithreaded applications is to have one Pattern instance per expression(a singleton), and one Matcher object per thread.
-
-
Field Summary
Fields Modifier and Type Field Description static intACCEPT_INCOMPLETEExperimental option; if a text ends up before the end of a pattern,report a match.static intANCHOR_ENDThe same effect as "$" without REFlags.MULTILINE.static intANCHOR_LASTMATCHThe same effect as "\\G".static intANCHOR_STARTThe same effect as "^" without REFlags.MULTILINE.-
Fields inherited from interface totalcross.util.regex.MatchResult
MATCH, PREFIX, SUFFIX, TARGET
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description charcharAt(int i)charcharAt(int i, int groupId)intend()intend(int id)booleanfind()Searches through a target for a matching substring, starting from just after the end of last match.booleanfind(int anchors)Searches through a target for a matching substring, starting from just after the end of last match.MatchIteratorfindAll()The same as findAll(int), but with default behaviour;MatchIteratorfindAll(int options)Returns an iterator over the matches found by subsequently calling find(options), the search starts from the zero position.booleangetGroup(int n, java.lang.StringBuffer sb)booleangetGroup(int n, TextBuffer tb)booleangetGroup(java.lang.String name, java.lang.StringBuffer sb)booleangetGroup(java.lang.String name, TextBuffer tb)java.lang.Stringgroup(int n)java.lang.Stringgroup(java.lang.String name)intgroupCount()java.lang.String[]groups()Vectorgroupv()booleanisCaptured()booleanisCaptured(int id)booleanisCaptured(java.lang.String groupName)booleanisStart()Deprecated.Replaced by isPrefix()intlength()intlength(int id)booleanmatches()Tells whether a current target matches the whole pattern.booleanmatches(java.lang.String s)Just a combination of setTarget(String) and matches().booleanmatchesPrefix()Tells whether the entire target matches the beginning of the pattern.Patternpattern()java.lang.Stringprefix()booleanproceed()Continues to search from where the last search left off.booleanproceed(int options)Continues to search from where the last search left off using specified options:voidsetPosition(int pos)Allows to set a position the subsequent find()/find(int) will start from.voidsetTarget(char[] text, int start, int len)Supplies a text to search in/match with, as a part of char array.voidsetTarget(char[] text, int start, int len, boolean shared)To be used with much care.voidsetTarget(java.lang.String text)Supplies a text to search in/match with.voidsetTarget(java.lang.String text, int start, int len)Supplies a text to search in/match with, as a part of String.voidsetTarget(CharStream in, int len)Supplies a text to search in/match with through a stream.voidsetTarget(Matcher m, int groupId)This method allows to efficiently pass data between matchers.voidskip()Sets the current search position just after the end of last match.intstart()intstart(int id)java.lang.Stringsuffix()java.lang.Stringtarget()char[]targetChars()inttargetEnd()inttargetStart()java.lang.StringtoString()java.lang.StringtoString_d()
-
-
-
Field Detail
-
ANCHOR_START
public static final int ANCHOR_START
The same effect as "^" without REFlags.MULTILINE.- See Also:
find(int), Constant Field Values
-
ANCHOR_LASTMATCH
public static final int ANCHOR_LASTMATCH
The same effect as "\\G".- See Also:
find(int), Constant Field Values
-
ANCHOR_END
public static final int ANCHOR_END
The same effect as "$" without REFlags.MULTILINE.- See Also:
find(int), Constant Field Values
-
ACCEPT_INCOMPLETE
public static final int ACCEPT_INCOMPLETE
Experimental option; if a text ends up before the end of a pattern,report a match.- See Also:
find(int), Constant Field Values
-
-
Method Detail
-
setTarget
public final void setTarget(Matcher m, int groupId)
This method allows to efficiently pass data between matchers. Note that a matcher may pass data to itself: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.- Parameters:
m- - a matcher that is a source of datagroupId- - which group to take data from- See Also:
setTarget(java.lang.String),setTarget(java.lang.String,int,int),setTarget(char[],int,int)
-
setTarget
public void setTarget(java.lang.String text)
Supplies a text to search in/match with. Resets current search position to zero.- Parameters:
text- - a data- See Also:
setTarget(totalcross.util.regex.Matcher,int),setTarget(java.lang.String,int,int),setTarget(char[],int,int)
-
setTarget
public void setTarget(java.lang.String text, int start, int len)Supplies a text to search in/match with, as a part of String. Resets current search position to zero.- Parameters:
text- - a data sourcestart- - where the target startslen- - how long is the target- See Also:
setTarget(totalcross.util.regex.Matcher,int),setTarget(java.lang.String),setTarget(char[],int,int)
-
setTarget
public void setTarget(char[] text, int start, int len)Supplies a text to search in/match with, as a part of char array. Resets current search position to zero.- Parameters:
text- - a data sourcestart- - where the target startslen- - how long is the target- See Also:
setTarget(totalcross.util.regex.Matcher,int),setTarget(java.lang.String),setTarget(java.lang.String,int,int)
-
setTarget
public final void setTarget(char[] text, int start, int len, boolean shared)To be used with much care. Supplies a text to search in/match with, as a part of a char array, as above, but also allows to permit to use the array as internal buffer for subsequent inputs. That is, if we call it withshared=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 permitted
then 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.- Parameters:
text- - a data sourcestart- - where the target startslen- - how long is the targetshared- - iftrue: data are shared or used later, don't modify it; iffalse: possible modifications of the text on subsequentsetTarget()calls are perceived and allowed.- See Also:
setTarget(totalcross.util.regex.Matcher,int),setTarget(java.lang.String),setTarget(java.lang.String,int,int),setTarget(char[],int,int)
-
setTarget
public void setTarget(CharStream in, int len) throws IOException
Supplies a text to search in/match with through a stream. Resets current search position to zero.- Parameters:
in- - a data stream;len- - how much characters should be read; if len is -1, read the entire stream.- Throws:
IOException- See Also:
setTarget(totalcross.util.regex.Matcher,int),setTarget(java.lang.String),setTarget(java.lang.String,int,int),setTarget(char[],int,int)
-
matchesPrefix
public final boolean matchesPrefix()
Tells whether the entire target matches the beginning of the pattern. The whole pattern is also regarded as its beginning.
This feature allows to find a mismatch by examining only a beginning part of the target (as if the beginning of the target doesn't match the beginning of the pattern, then the entire target also couldn't match).
For example the following assertions yieldtrue: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 yieldfalse:p.matcher("b").isPrefix(); p.matcher("abcdef").isPrefix(); p.matcher("x").isPrefix();- Returns:
- true if the entire target matches the beginning of the pattern
-
isStart
@Deprecated public final boolean isStart()
Deprecated.Replaced by isPrefix()Just an old name for isPrefix().
Retained for backwards compatibility.
-
matches
public final boolean matches()
Tells whether a current target matches the whole pattern. For example the following yields thetrue:Pattern p=new Pattern("\\w+"); p.matcher("a").matches(); p.matcher("ab").matches(); p.matcher("abc").matches();and the following yields thefalse: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.
-
matches
public final boolean matches(java.lang.String s)
Just a combination of setTarget(String) and matches().- Parameters:
s- the target string;- Returns:
- whether the specified string matches the whole pattern.
-
setPosition
public void setPosition(int pos)
Allows to set a position the subsequent find()/find(int) will start from.
-
find
public final boolean find()
Searches through a target for a matching substring, starting from just after the end of last match. If there wasn't any search performed, starts from zero.- Returns:
trueif a match found.
-
find
public final boolean find(int anchors)
Searches through a target for a matching substring, starting from just after the end of last match. If there wasn't any search performed, starts from zero.- Parameters:
anchors- a zero or a combination(bitwise OR) of ANCHOR_START,ANCHOR_END,ANCHOR_LASTMATCH,ACCEPT_INCOMPLETE- Returns:
trueif a match found.
-
findAll
public MatchIterator findAll()
The same as findAll(int), but with default behaviour;
-
findAll
public MatchIterator findAll(int options)
Returns an iterator over the matches found by subsequently calling find(options), the search starts from the zero position.
-
proceed
public final boolean proceed()
Continues to search from where the last search left off. The same as proceed(0).- See Also:
proceed(int)
-
proceed
public final 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 usingfind()method we would find '123' only.- Parameters:
options- search options, some of ANCHOR_START|ANCHOR_END|ANCHOR_LASTMATCH|ACCEPT_INCOMPLETE; zero value(default) stands for usual search for substring.
-
skip
public final void skip()
Sets the current search position just after the end of last match.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
pattern
public Pattern pattern()
- Specified by:
patternin interfaceMatchResult
-
target
public java.lang.String target()
- Specified by:
targetin interfaceMatchResult
-
targetChars
public char[] targetChars()
- Specified by:
targetCharsin interfaceMatchResult
-
targetStart
public int targetStart()
- Specified by:
targetStartin interfaceMatchResult
-
targetEnd
public int targetEnd()
- Specified by:
targetEndin interfaceMatchResult
-
charAt
public char charAt(int i)
- Specified by:
charAtin interfaceMatchResult
-
charAt
public char charAt(int i, int groupId)- Specified by:
charAtin interfaceMatchResult
-
length
public final int length()
- Specified by:
lengthin interfaceMatchResult
-
start
public final int start()
- Specified by:
startin interfaceMatchResult
-
end
public final int end()
- Specified by:
endin interfaceMatchResult
-
prefix
public java.lang.String prefix()
- Specified by:
prefixin interfaceMatchResult
-
suffix
public java.lang.String suffix()
- Specified by:
suffixin interfaceMatchResult
-
groupCount
public int groupCount()
- Specified by:
groupCountin interfaceMatchResult
-
group
public java.lang.String group(int n)
- Specified by:
groupin interfaceMatchResult
-
group
public java.lang.String group(java.lang.String name)
- Specified by:
groupin interfaceMatchResult
-
getGroup
public boolean getGroup(int n, TextBuffer tb)- Specified by:
getGroupin interfaceMatchResult
-
getGroup
public boolean getGroup(java.lang.String name, TextBuffer tb)- Specified by:
getGroupin interfaceMatchResult
-
getGroup
public boolean getGroup(int n, java.lang.StringBuffer sb)- Specified by:
getGroupin interfaceMatchResult
-
getGroup
public boolean getGroup(java.lang.String name, java.lang.StringBuffer sb)- Specified by:
getGroupin interfaceMatchResult
-
groups
public java.lang.String[] groups()
-
groupv
public Vector groupv()
-
isCaptured
public final boolean isCaptured()
- Specified by:
isCapturedin interfaceMatchResult
-
isCaptured
public final boolean isCaptured(int id)
- Specified by:
isCapturedin interfaceMatchResult
-
isCaptured
public final boolean isCaptured(java.lang.String groupName)
- Specified by:
isCapturedin interfaceMatchResult
-
length
public final int length(int id)
- Specified by:
lengthin interfaceMatchResult
-
start
public final int start(int id)
- Specified by:
startin interfaceMatchResult
-
end
public final int end(int id)
- Specified by:
endin interfaceMatchResult
-
toString_d
public java.lang.String toString_d()
-
-