SearchIterator defines a protocol for text searching. Subclasses provide concrete implementations of various search algorithms. For example, StringSearch implements language-sensitive pattern matching based on the comparison rules defined in a RuleBasedCollator object.
Other options for searching include using a BreakIterator to restrict the points at which matches are detected.
SearchIterator provides an API that is similar to that of other text iteration classes such as BreakIterator. Using this class, it is easy to scan through text looking for all occurrences of a given pattern. The following example uses a StringSearch object to find all instances of "fox" in the target string. Any other subclass of SearchIterator can be used in an identical manner.
String target = "The quick brown fox jumped over the lazy fox";
String pattern = "fox";
SearchIterator iter = new StringSearch(pattern, target);
for (int pos = iter.first(); pos != SearchIterator.DONE;
pos = iter.next()) {
System.out.println("Found match at " + pos +
", length is " + iter.getMatchLength());
}
BreakIterator, RuleBasedCollator
| Modifier and Type | Class and Description |
|---|---|
| public static enum | SearchIterator.ElementComparisonType
Option to control how collation elements are compared. |
| pack-priv class | SearchIterator.Search
Java port of ICU4C struct USearch (usrchimp.h) Note ICU4J already exposed some protected members such as targetText, breakIterator and matchedLength as a part of stable APIs. |
| Modifier and Type | Field and Description |
|---|---|
| protected BreakIterator | breakIterator
The BreakIterator to define the boundaries of a logical match. |
| public static final int | DONE
DONE is returned by previous() and next() after all valid matches have been returned, and by first() and last() if there are no matches at all. |
| protected int | matchLength
Length of the most current match in target text. |
| pack-priv SearchIterator. | |
| protected CharacterIterator | targetText
Target text for searching. |
| Access | Constructor and Description |
|---|---|
| protected | SearchIterator(CharacterIterator
The target text to be searched. target, BreakIterator A breaker)BreakIterator that is used to determine the
boundaries of a logical match. This argument can be null.Protected constructor for use by subclasses. |
| Modifier and Type | Method and Description |
|---|---|
| public final int | |
| public final int | |
| public BreakIterator | Returns: the BreakIterator set to restrict logic matchesReturns the BreakIterator that is used to restrict the indexes at which matches are detected. |
| public SearchIterator. | |
| public abstract int | Returns: current index in the text being searched.Return the current index in the text being searched. |
| public String | Returns: the substring in the target test of the most recent match, or null if there is no match currently.Returns the text that was matched by the most recent call to
|
| public int | Returns: The length of the match in the target text, or 0 if there is no match currently.Returns the length of text in the string which matches the search pattern. |
| public int | Returns: index of a substring within the text string that is being searched.Returns the index to the match in the text string that was searched. |
| public CharacterIterator | |
| protected abstract int | Returns: index at which the match starts, else if match is not foundDONE is returnedThe index in the target text at which the search
should start. start)Abstract method which subclasses override to provide the mechanism for finding the next match in the target text. |
| protected abstract int | Returns: index at which the match starts, else if match is not foundDONE is returnedThe index in the target text at which the search
should start. startAt)Abstract method which subclasses override to provide the mechanism for finding the previous match in the target text. |
| public boolean | Returns: true if the overlapping property has been set, false otherwiseReturn true if the overlapping property has been set. |
| public final int | |
| public int | Returns: The index of the next match after the current position, orDONE if there are no more matches.Returns the index of the next point at which the text matches the
search pattern, starting from the current position
The iterator is adjusted so that its current index (as returned by
|
| public final int | |
| public int | |
| public void | |
| public void | setBreakIterator(BreakIterator
A BreakIterator that will be used to restrict the
points at which matches are detected. If a match is
found, but the match's start or end index is not a
boundary as determined by the breakiter)BreakIterator,
the match will be rejected and another will be searched
for. If this parameter is null, no break
detection is attempted.Set the BreakIterator that will be used to restrict the points at which matches are detected. |
| public void | setElementComparisonType(SearchIterator.
Sets the collation element comparison type. |
| public void | setIndex(int
position from which to start the next search position)Sets the position in the target text at which the next search will start. |
| protected void | setMatchLength(int
new length to set length)Sets the length of the most recent match in the target text. |
| protected void | |
| public void | setOverlapping(boolean
flag indicator if overlapping matches are allowed allowOverlap)Determines whether overlapping matches are returned. |
| public void | setTarget(CharacterIterator
new text iterator to look for match, text)Set the target text to be searched. |
| breakIterator | back to summary |
|---|---|
| protected BreakIterator breakIterator The BreakIterator to define the boundaries of a logical match. This value can be a null. See class documentation for more information.
| |
| DONE | back to summary |
|---|---|
| public static final int DONE DONE is returned by previous() and next() after all valid matches have been returned, and by first() and last() if there are no matches at all. | |
| matchLength | back to summary |
|---|---|
| protected int matchLength Length of the most current match in target text. Value 0 is the default value.
| |
| search_ | back to summary |
|---|---|
| pack-priv SearchIterator. | |
| targetText | back to summary |
|---|---|
| protected CharacterIterator targetText Target text for searching.
| |
| SearchIterator | back to summary |
|---|---|
| protected SearchIterator(CharacterIterator target, BreakIterator breaker) Protected constructor for use by subclasses.
Initializes the iterator with the argument target text for searching
and sets the BreakIterator.
See class documentation for more details on the use of the target text
and
| |
| first | back to summary |
|---|---|
| public final int first() Returns the first index at which the string text matches the search
pattern. The iterator is adjusted so that its current index (as
returned by | |
| following | back to summary |
|---|---|
| public final int following(int position) Returns the first index equal or greater than position at which the
string text matches the search pattern. The iterator is adjusted so
that its current index (as returned by
| |
| getBreakIterator | back to summary |
|---|---|
| public BreakIterator getBreakIterator() Returns the BreakIterator that is used to restrict the indexes at which
matches are detected. This will be the same object that was passed to
the constructor or to
| |
| getElementComparisonType | back to summary |
|---|---|
| public SearchIterator. Returns the collation element comparison type.
| |
| getIndex | back to summary |
|---|---|
| public abstract int getIndex() Return the current index in the text being searched.
If the iteration has gone past the end of the text
(or past the beginning for a backwards search),
| |
| getMatchedText | back to summary |
|---|---|
| public String getMatchedText() Returns the text that was matched by the most recent call to
| |
| getMatchLength | back to summary |
|---|---|
| public int getMatchLength() Returns the length of text in the string which matches the search
pattern. This call returns a valid result only after a successful call
to | |
| getMatchStart | back to summary |
|---|---|
| public int getMatchStart() Returns the index to the match in the text string that was searched.
This call returns a valid result only after a successful call to
Use | |
| getTarget | back to summary |
|---|---|
| public CharacterIterator getTarget() Return the string text to be searched.
| |
| handleNext | back to summary |
|---|---|
| protected abstract int handleNext(int start) Abstract method which subclasses override to provide the mechanism for finding the next match in the target text. This allows different subclasses to provide different search algorithms.
If a match is found, the implementation should return the index at
which the match starts and should call
| |
| handlePrevious | back to summary |
|---|---|
| protected abstract int handlePrevious(int startAt) Abstract method which subclasses override to provide the mechanism for finding the previous match in the target text. This allows different subclasses to provide different search algorithms.
If a match is found, the implementation should return the index at
which the match starts and should call
| |
| isOverlapping | back to summary |
|---|---|
| public boolean isOverlapping() Return true if the overlapping property has been set.
See
| |
| last | back to summary |
|---|---|
| public final int last() Returns the last index in the target text at which it matches the
search pattern. The iterator is adjusted so that its current index
(as returned by | |
| next | back to summary |
|---|---|
| public int next() Returns the index of the next point at which the text matches the
search pattern, starting from the current position
The iterator is adjusted so that its current index (as returned by
| |
| preceding | back to summary |
|---|---|
| public final int preceding(int position) Returns the first index less than position at which the string
text matches the search pattern. The iterator is adjusted so that its
current index (as returned by
When the overlapping option (
| |
| previous | back to summary |
|---|---|
| public int previous() Returns the index of the previous point at which the string text
matches the search pattern, starting at the current position.
The iterator is adjusted so that its current index (as returned by
| |
| reset | back to summary |
|---|---|
| public void reset() Resets the iteration. Search will begin at the start of the text string if a forward iteration is initiated before a backwards iteration. Otherwise if a backwards iteration is initiated before a forwards iteration, the search will begin at the end of the text string.
| |
| setBreakIterator | back to summary |
|---|---|
| public void setBreakIterator(BreakIterator breakiter) Set the BreakIterator that will be used to restrict the points at which matches are detected.
| |
| setElementComparisonType | back to summary |
|---|---|
| public void setElementComparisonType(SearchIterator. Sets the collation element comparison type.
The default comparison type is
| |
| setIndex | back to summary |
|---|---|
| public void setIndex(int position) Sets the position in the target text at which the next search will start. This method clears any previous match.
| |
| setMatchLength | back to summary |
|---|---|
| protected void setMatchLength(int length) Sets the length of the most recent match in the target text. Subclasses' handleNext() and handlePrevious() methods should call this after they find a match in the target text.
| |
| setMatchNotFound | back to summary |
|---|---|
| protected void setMatchNotFound()
Deprecated This API is ICU internal only.
| |
| setOverlapping | back to summary |
|---|---|
| public void setOverlapping(boolean allowOverlap) Determines whether overlapping matches are returned. See the class documentation for more information about overlapping matches. The default setting of this property is false
| |
| setTarget | back to summary |
|---|---|
| public void setTarget(CharacterIterator text) Set the target text to be searched. Text iteration will then begin at the start of the text string. This method is useful if you want to reuse an iterator to search within a different body of text.
| |
STANDARD_ELEMENT_COMPARISON.
PATTERN_BASE_WEIGHT_IS_WILDCARD supports "asymmetric search" as described in UTS #10 Unicode Collation Algorithm, while ANY_BASE_WEIGHT_IS_WILDCARD supports a related option in which "unmarked" characters in either the pattern or the searched text are treated as wildcards that match marked or unmarked versions of the same character.
setElementComparisonType(ElementComparisonType), getElementComparisonType()
| Modifier and Type | Field and Description |
|---|---|
| public static final SearchIterator. | ANY_BASE_WEIGHT_IS_WILDCARD
Collation element comparison is modified to effectively provide behavior between the specified strength and strength - 1. |
| public static final SearchIterator. | PATTERN_BASE_WEIGHT_IS_WILDCARD
Collation element comparison is modified to effectively provide behavior between the specified strength and strength - 1. |
| public static final SearchIterator. | STANDARD_ELEMENT_COMPARISON
Standard collation element comparison at the specified collator strength. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static SearchIterator. | |
| public static SearchIterator. |
| ANY_BASE_WEIGHT_IS_WILDCARD | back to summary |
|---|---|
| public static final SearchIterator. Collation element comparison is modified to effectively provide behavior between the specified strength and strength - 1. Collation elements in either the pattern or the searched text that have the base weight for the specified strength are treated as "wildcards" that match an element with any other weight at that collation level. For example, with a secondary-strength English collator, a plain 'e' in the pattern will match a plain e or an e with any diacritic in the searched text, but an e with diacritic in the pattern will only match an e with the same diacritic or a plain e in the searched text.
| |
| PATTERN_BASE_WEIGHT_IS_WILDCARD | back to summary |
|---|---|
| public static final SearchIterator. Collation element comparison is modified to effectively provide behavior between the specified strength and strength - 1. Collation elements in the pattern that have the base weight for the specified strength are treated as "wildcards" that match an element with any other weight at that collation level in the searched text. For example, with a secondary-strength English collator, a plain 'e' in the pattern will match a plain e or an e with any diacritic in the searched text, but an e with diacritic in the pattern will only match an e with the same diacritic in the searched text.
| |
| STANDARD_ELEMENT_COMPARISON | back to summary |
|---|---|
| public static final SearchIterator. Standard collation element comparison at the specified collator strength.
| |
| ElementComparisonType | back to summary |
|---|---|
| private ElementComparisonType() | |
| valueOf | back to summary |
|---|---|
| public static SearchIterator. | |
| values | back to summary |
|---|---|
| public static SearchIterator. | |
Note
ICU4J already exposed some protected members such as targetText, breakIterator and matchedLength as a part of stable APIs. In ICU4C, they are exposed through USearch struct, although USearch struct itself is internal API. This class was created for making ICU4J code parallel to ICU4C implementation. ICU4J implementation access member fields like C struct (e.g. search_.isOverlap_) mostly, except fields already exposed as protected member (e.g. search_.text()).
| Modifier and Type | Field and Description |
|---|---|
| pack-priv SearchIterator. | |
| pack-priv BreakIterator | |
| pack-priv boolean | |
| pack-priv boolean | isForwardSearching_
Flag indicates if we are doing a forwards search |
| pack-priv boolean | isOverlap_
Flag to indicate if overlapping search is to be done. |
| pack-priv int | |
| pack-priv boolean | reset_
Flag indicates if we are at the start of a string search. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | |
| pack-priv BreakIterator | |
| pack-priv int | |
| pack-priv int | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv CharacterIterator |
| elementComparisonType_ | back to summary |
|---|---|
| pack-priv SearchIterator. | |
| internalBreakIter_ | back to summary |
|---|---|
| pack-priv BreakIterator internalBreakIter_ | |
| isCanonicalMatch_ | back to summary |
|---|---|
| pack-priv boolean isCanonicalMatch_ | |
| isForwardSearching_ | back to summary |
|---|---|
| pack-priv boolean isForwardSearching_ Flag indicates if we are doing a forwards search | |
| isOverlap_ | back to summary |
|---|---|
| pack-priv boolean isOverlap_ Flag to indicate if overlapping search is to be done. E.g. looking for "aa" in "aaa" will yield matches at offset 0 and 1. | |
| matchedIndex_ | back to summary |
|---|---|
| pack-priv int matchedIndex_ | |
| reset_ | back to summary |
|---|---|
| pack-priv boolean reset_ Flag indicates if we are at the start of a string search. This indicates that we are in forward search and at the start of m_text. | |
| Search | back to summary |
|---|---|
| pack-priv Search() | |
| beginIndex | back to summary |
|---|---|
| pack-priv int beginIndex() | |
| breakIter | back to summary |
|---|---|
| pack-priv BreakIterator breakIter() | |
| endIndex | back to summary |
|---|---|
| pack-priv int endIndex() | |
| matchedLength | back to summary |
|---|---|
| pack-priv int matchedLength() | |
| setBreakIter | back to summary |
|---|---|
| pack-priv void setBreakIter(BreakIterator breakIter) | |
| setMatchedLength | back to summary |
|---|---|
| pack-priv void setMatchedLength(int matchedLength) | |
| setTarget | back to summary |
|---|---|
| pack-priv void setTarget(CharacterIterator text) | |
| text | back to summary |
|---|---|
| pack-priv CharacterIterator text() | |