Class StringMatcher
- Since:
- 3.12
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classStart and end positions of a shortest match found byfind(String, int, int). -
Constructor Summary
ConstructorsConstructorDescriptionStringMatcher(String pattern, boolean ignoreCase, boolean ignoreWildCards) StringMatcher constructor takes in a String object that is a simple pattern. -
Method Summary
Modifier and TypeMethodDescriptionFinds the first occurrence of the pattern betweenstart(inclusive) andend(exclusive).static String[]Splits a given text into words.booleanDetermines whether the giventextmatches the pattern.booleanDetermines whether the given sub-string oftextfromstart(inclusive) toend(exclusive) matches the pattern.booleanmatchWords(String text) Similar tomatch(String), this methods matches a pattern that may contain the wildcards '?'toString()voidConfigures thisStringMatcherto also match on prefix-only matches.
-
Constructor Details
-
StringMatcher
StringMatcher constructor takes in a String object that is a simple pattern. The pattern may contain '*' for 0 and many characters and '?' for exactly one character.Literal '*' and '?' characters must be escaped in the pattern e.g., "\*" means literal "*", etc.
The escape character '\' is an escape only if followed by '*', '?', or '\'. All other occurrences are taken literally.
If invoking the StringMatcher with string literals in Java, don't forget escape characters are represented by "\\".
An empty pattern matches only an empty text, unless
usePrefixMatch()is used, in which case it always matches.- Parameters:
pattern- the pattern to match text against, must not benullignoreCase- if true, case is ignoredignoreWildCards- if true, wild cards and their escape sequences are ignored (everything is taken literally).- Throws:
IllegalArgumentException- ifpattern == null
-
-
Method Details
-
usePrefixMatch
public void usePrefixMatch()Configures thisStringMatcherto also match on prefix-only matches.If the matcher was created with
ignoreWildCards == true, any wildcard characters in the pattern will still be matched literally.If the pattern is empty, it will match any text.
- Since:
- 3.13
-
find
Finds the first occurrence of the pattern betweenstart(inclusive) andend(exclusive).If wildcards are enabled: If the pattern contains only '*' wildcards a full match is reported, otherwise leading and trailing '*' wildcards are ignored. If the pattern contains interior '*' wildcards, the first shortest match is returned.
- Parameters:
text- the String object to search in; must not benullstart- the starting index of the search range, inclusiveend- the ending index of the search range, exclusive- Returns:
- a
StringMatcher.Positionobject for the match found, ornullif there's no match or the text range to search is empty (end <= start). If the pattern is empty, the position will describe a null-match in thetextatstart(getStart()==getEnd()==start).
Note: for patterns like "*abc*" with leading and trailing stars, the position of "abc" is returned. For a pattern like"*??*" in text "abcdf", (0,2) is returned. Interior '*'s yield the shortest match: for pattern "a*b" and text "axbyb", (0,3) is returned, not (0,5). - Throws:
IllegalArgumentException- iftext == null
-
match
Determines whether the giventextmatches the pattern.- Parameters:
text- String to match; must not benull- Returns:
trueif the wholetextmatches the pattern;falseotherwise- Throws:
IllegalArgumentException- iftext == null
-
match
Determines whether the given sub-string oftextfromstart(inclusive) toend(exclusive) matches the pattern.- Parameters:
text- String to match in; must not benullstart- start index (inclusive) withintextof the sub-string to matchend- end index (exclusive) withintextof the sub-string to match- Returns:
trueif the given slice oftextmatches the pattern;falseotherwise- Throws:
IllegalArgumentException- iftext == null
-
matchWords
Similar tomatch(String), this methods matches a pattern that may contain the wildcards '?' or '*' against a text. However, the matching is not only done on the full text, but also on individual words from the text, and if the pattern contains whitespace, the pattern is split into sub-patterns and those are matched, too.The precise rules are:
- If the full pattern matches the full text, the match succeeds.
- If the full pattern matches a single word of the text, the match succeeds.
- If all sub-patterns match a prefix of the whole text or any prefix of any word, the match succeeds.
- Otherwise, the match fails.
An empty pattern matches only the empty text.
- Since:
- 3.20
-
getWords
Splits a given text into words.- Parameters:
text- to split- Returns:
- the words of the text
- Since:
- 3.20
-
toString
-