Class StringMatcher
- Since:
- 3.12
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Start 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.boolean
Determines whether the giventext
matches the pattern.boolean
Determines whether the given sub-string oftext
fromstart
(inclusive) toend
(exclusive) matches the pattern.boolean
matchWords
(String text) Similar tomatch(String)
, this methods matches a pattern that may contain the wildcards '?'toString()
void
Configures thisStringMatcher
to 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 benull
ignoreCase
- 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 thisStringMatcher
to 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 benull
start
- the starting index of the search range, inclusiveend
- the ending index of the search range, exclusive- Returns:
- a
StringMatcher.Position
object for the match found, ornull
if 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 thetext
atstart
(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 giventext
matches the pattern.- Parameters:
text
- String to match; must not benull
- Returns:
true
if the wholetext
matches the pattern;false
otherwise- Throws:
IllegalArgumentException
- iftext == null
-
match
Determines whether the given sub-string oftext
fromstart
(inclusive) toend
(exclusive) matches the pattern.- Parameters:
text
- String to match in; must not benull
start
- start index (inclusive) withintext
of the sub-string to matchend
- end index (exclusive) withintext
of the sub-string to match- Returns:
true
if the given slice oftext
matches the pattern;false
otherwise- 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
-