Class TextMatcher
- All Implemented Interfaces:
BiFunction<String,,String, String> BinaryOperator<String>,Predicate<CharSequence>
A configurable class offering a few different ways to perform text matching and replacing. Supported methods are:
- BASIC: Default. Text is matched as specified.
- CSV: Same has having multiple BASIC, separated with commas.
- WILDCARD: An asterisk (*) matches a series made of any characters. A question mark (?) matches any single character. If you need to escape wildcards, use REGEX instead.
- REGEX: Matching/replacing is done using Java-style regular expressions.
Match/replace methods are case-sensitive by default.
This class is not thread-safe.
Empty and null values
Since 3.0.0, null or empty strings are not matched by
default. To have those considered as positive matches, set
matchEmpty to true. To have blank values
(containing white spaces only) considered as positive matches,
also set trim to true.
When matching empties, doing replacement on a null value
behaves as if the value is an empty string.
null pattern
Unless otherwise stated by consuming classes,
a null expressions will match everything, but replace nothing.
when invoking matches(CharSequence) or
replace(String, String).
- Since:
- 2.0.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a basic matcher.TextMatcher(TextMatcher textMatcher) Copy constructor.TextMatcher(TextMatcher.Method method) Creates a matcher with the specified method.TextMatcher(String pattern) Creates a basic matcher with the given pattern.TextMatcher(String pattern, TextMatcher.Method method) Creates a basic matcher with the given pattern. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleananyMatches(List<TextMatcher> matchers, CharSequence text) Tests that at least one matcher matches the provided text.static booleananyMatchesOrEmpty(List<TextMatcher> matchers, CharSequence text) Tests that at least one matcher matches the provided text, treating an empty ornulllist as a match (returningtrue).Replaces this class matching text with replacement value.static TextMatcherCreates a new text matcher initialized with basic matching.protected booleanvoidcopyFrom(TextMatcher tm) voidcopyTo(TextMatcher tm) static TextMatcherCreates a new text matcher initialized with comma-separated-value matching.booleaninthashCode()booleanbooleanbooleanGets whethernullor empty strings should be considered a positive match.booleanWhether to negates the result of invokingmatches(CharSequence).booleanbooleanbooleanisSet()Checks whether this text matcher was given a pattern.booleanisTrim()Gets whether values should be trimmed before being evaluated (as perString.trim()).Sets thatnullor empty strings should be considered a positive match.booleanmatches(CharSequence text) Matches this class pattern against its text.Sets the negation of the result of invokingmatches(CharSequence)totrue.partial()static TextMatcherCreates a new text matcher initialized with regular expression matching.Replaces this class matching text with replacement value.setIgnoreCase(boolean ignoreCase) setIgnoreDiacritic(boolean ignoreDiacritic) setMatchEmpty(boolean matchEmpty) Sets whethernullor empty strings should be considered a positive match.setMethod(TextMatcher.Method method) setNegateMatches(boolean negateMatches) Sets whether to negates the result of invokingmatches(CharSequence).setPartial(boolean partial) setPattern(String pattern) setReplaceAll(boolean replaceAll) setTrim(boolean trim) Sets whether values should be trimmed before being evaluated (as perString.trim()).booleantest(CharSequence text) Matches this class pattern against its text.toRegexMatcher(CharSequence text) Converts this text matcher to a patternMatcher.Compiles this text matcher to create a regular expressionPattern.toString()trim()Sets that values should be trimmed before being evaluated (as perString.trim()).static TextMatcherCreates a new text matcher initialized with wildcard matching.withIgnoreCase(boolean ignoreCase) withIgnoreDiacritic(boolean ignoreDiacritic) withMatchEmpty(boolean matchEmpty) Sets whethernullor empty strings should be considered a positive match.withMethod(TextMatcher.Method method) withNegateMatches(boolean negateMatches) Sets whether to negates the result of invokingmatches(CharSequence)on a copy of this instance.withPartial(boolean partial) withPattern(String pattern) withReplaceAll(boolean replaceAll) withTrim(boolean trim) Sets whether values should be trimmed before being evaluated (as perString.trim()).Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.function.BiFunction
andThen
-
Constructor Details
-
TextMatcher
public TextMatcher()Creates a basic matcher. -
TextMatcher
Creates a basic matcher with the given pattern. Default behavior will match the pattern exactly.- Parameters:
pattern- expression used for matching
-
TextMatcher
Creates a matcher with the specified method.- Parameters:
method- matching method
-
TextMatcher
Creates a basic matcher with the given pattern.- Parameters:
pattern- expression used for matchingmethod- matching method
-
TextMatcher
Copy constructor. Supplyingnullis the same as evoking the empty constructor.- Parameters:
textMatcher- instance to copy
-
-
Method Details
-
getMethod
-
setMethod
-
withMethod
-
isPartial
public boolean isPartial() -
setPartial
-
partial
-
withPartial
-
getPattern
-
setPattern
-
withPattern
-
isIgnoreCase
public boolean isIgnoreCase() -
setIgnoreCase
-
ignoreCase
-
withIgnoreCase
-
isIgnoreDiacritic
public boolean isIgnoreDiacritic() -
setIgnoreDiacritic
-
ignoreDiacritic
-
withIgnoreDiacritic
-
isReplaceAll
public boolean isReplaceAll() -
setReplaceAll
-
replaceAll
-
withReplaceAll
-
isNegateMatches
public boolean isNegateMatches()Whether to negates the result of invokingmatches(CharSequence). Note: only applies to thematches(CharSequence)method.- Returns:
trueif negating matches.- Since:
- 3.0.0
-
setNegateMatches
Sets whether to negates the result of invokingmatches(CharSequence). Note: only applies to thematches(CharSequence)method.- Parameters:
negateMatches-trueto negate matches- Returns:
- this
- Since:
- 3.0.0
-
negateMatches
Sets the negation of the result of invokingmatches(CharSequence)totrue. Same as invokingsetNegateMatches(boolean)withtrue. Note: only applies to thematches(CharSequence)method.- Returns:
- this
- Since:
- 3.0.0
-
withNegateMatches
Sets whether to negates the result of invokingmatches(CharSequence)on a copy of this instance. Note: only applies to thematches(CharSequence)method.- Parameters:
negateMatches-trueto negate matches- Returns:
- this
- Since:
- 3.0.0
-
isSet
public boolean isSet()Checks whether this text matcher was given a pattern.- Returns:
trueif a pattern is set (i.e., notnull).
-
isMatchEmpty
public boolean isMatchEmpty()Gets whethernullor empty strings should be considered a positive match.- Returns:
trueifnulland empty strings are considered a match- Since:
- 3.0.0
-
setMatchEmpty
Sets whethernullor empty strings should be considered a positive match. To also consider blank values as positive matches, usesetTrim(boolean).- Parameters:
matchEmpty-trueto havenulland empty strings are considered a match.- Returns:
- this instance
- Since:
- 3.0.0
-
matchEmpty
Sets thatnullor empty strings should be considered a positive match. Same as invokingsetMatchEmpty(boolean)withtrue.- Returns:
- this instance
- Since:
- 3.0.0
-
withMatchEmpty
Sets whethernullor empty strings should be considered a positive match. To also consider blank values as positive matches, usesetTrim(boolean).- Parameters:
matchEmpty-trueto havenulland empty strings are considered a match.- Returns:
- a copy of this instance, leaving this instance unchanged
- Since:
- 3.0.0
-
isTrim
public boolean isTrim()Gets whether values should be trimmed before being evaluated (as perString.trim()).- Returns:
trueif values are trimmed before evaluation- Since:
- 3.0.0
-
setTrim
Sets whether values should be trimmed before being evaluated (as perString.trim()).- Parameters:
trim-trueto trim values before evaluation- Returns:
- this instance
- Since:
- 3.0.0
-
trim
Sets that values should be trimmed before being evaluated (as perString.trim()). Same as invokingsetTrim(boolean)withtrue.- Returns:
- this instance
- Since:
- 3.0.0
-
withTrim
Sets whether values should be trimmed before being evaluated (as perString.trim()).- Parameters:
trim-trueto trim values before evaluation- Returns:
- a copy of this instance, leaving this instance unchanged
- Since:
- 3.0.0
-
copyTo
-
copyFrom
-
basic
Creates a new text matcher initialized with basic matching. Same as invoking
new TextMatcher(Method.BASIC).setPattern(pattern).- Parameters:
pattern- expression to match against values- Returns:
- basic text matcher
-
csv
Creates a new text matcher initialized with comma-separated-value matching. Same as invoking
new TextMatcher(Method.CSV).setPattern(pattern).- Parameters:
pattern- expression to match against values- Returns:
- csv text matcher
-
wildcard
Creates a new text matcher initialized with wildcard matching. Same as invoking
new TextMatcher(Method.WILDCARD).setPattern(pattern).- Parameters:
pattern- expression to match against values- Returns:
- wildcard text matcher
-
regex
Creates a new text matcher initialized with regular expression matching. Same as invoking
new TextMatcher(Method.REGEX).setPattern(pattern).- Parameters:
pattern- expression to match against values- Returns:
- regex text matcher
-
test
Matches this class pattern against its text. For compatibility withPredicate. Same as invokingmatches(CharSequence).- Specified by:
testin interfacePredicate<CharSequence>- Parameters:
text- text to match- Returns:
trueif matching
-
matches
Matches this class pattern against its text. Anullpattern will match all.- Parameters:
text- text to match- Returns:
trueif matching
-
apply
Replaces this class matching text with replacement value. For compatibility withBinaryOperator. Same as invokingreplace(String, String).- Specified by:
applyin interfaceBiFunction<String,String, String> - Parameters:
text- text to matchreplacement- text replacement- Returns:
- replaced text
-
replace
Replaces this class matching text with replacement value.- Parameters:
text- text to matchreplacement- text replacement- Returns:
- replaced text
-
toRegexMatcher
Converts this text matcher to a patternMatcher.- Parameters:
text- text to match using this text matcher method- Returns:
- matcher
-
toRegexPattern
Compiles this text matcher to create a regular expressionPattern.- Returns:
- pattern
-
anyMatchesOrEmpty
Tests that at least one matcher matches the provided text, treating an empty ornulllist as a match (returningtrue).- Parameters:
matchers- matchers to test against supplied texttext- the text being tested- Returns:
trueif an empty list or at least one matcher matches the text- Since:
- 3.0.0
-
anyMatches
Tests that at least one matcher matches the provided text. If matchers is empty ornull, it is considered not to match (returnsfalse).- Parameters:
matchers- matchers to test against supplied texttext- the text being tested- Returns:
trueif at least one matcher matches the text- Since:
- 3.0.0
-
toString
-
equals
-
canEqual
-
hashCode
public int hashCode()
-