Class TextMatcher
- All Implemented Interfaces:
IXMLConfigurable,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.
XML configuration usage:
method="[basic|csv|wildcard|regex]"
ignoreCase="[false|true]"
ignoreDiacritic="[false|true]"
replaceAll="[false|true]"
partial="[false|true]"
The above are configurable attributes consuming classes can expect. The actual expression is expected to be the tag content.
Null handling
Unless otherwise stated by consuming classes,
a null expressions will match everything, but replace nothing.
when invoking matches(CharSequence) or
replace(String, String).
When simply matching (no replacements) the attributes are the same, minus the "replaceAll" (which is simply ignored):
method="[basic|csv|wildcard|regex]"
ignoreCase="[false|true]"
ignoreDiacritic="[false|true]"
partial="[false|true]"
XML usage example:
<sampleConfig
method="wildcard"
ignoreCase="true"
partial="true">
paul*mar?
</sampleConfig>
Given a text of "It seems Paul and Marc are friends" and a replacement of "they", the above will result in "It seems they are friends.".
- Since:
- 2.0.0
- Author:
- Pascal Essiembre
-
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 TypeMethodDescriptionReplaces this class matching text with replacement value.static TextMatcherCreates a new text matcher initialized with basic matching.voidcopyFrom(TextMatcher tm) voidcopyTo(TextMatcher tm) static TextMatcherCreates a new text matcher initialized with comma-separated-value matching.booleaninthashCode()booleanbooleanbooleanbooleanvoidloadFromXML(XML xml) Load XML configuration values and initialized this object with them.booleanmatches(CharSequence text) Matches this class pattern against its text.partial()static TextMatcherCreates a new text matcher initialized with regular expression matching.Replaces this class matching text with replacement value.voidSaves this object as XML.setIgnoreCase(boolean ignoreCase) setIgnoreDiacritic(boolean ignoreDiacritic) setMethod(TextMatcher.Method method) setPartial(boolean partial) setPattern(String pattern) setReplaceAll(boolean replaceAll) 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()static TextMatcherCreates a new text matcher initialized with wildcard matching.withIgnoreCase(boolean ignoreCase) withIgnoreDiacritic(boolean ignoreDiacritic) withMethod(TextMatcher.Method method) withPartial(boolean partial) withPattern(String pattern) withReplaceAll(boolean replaceAll) 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
-
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.- Parameters:
text- text to match- Returns:
trueif matching
-
apply
Replaces this class matching text with replacement value. For compatibility withBiFunction. 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
-
loadFromXML
Description copied from interface:IXMLConfigurableLoad XML configuration values and initialized this object with them.- Specified by:
loadFromXMLin interfaceIXMLConfigurable- Parameters:
xml- the XML to load into this object
-
saveToXML
Description copied from interface:IXMLConfigurableSaves this object as XML.- Specified by:
saveToXMLin interfaceIXMLConfigurable- Parameters:
xml- the XML that will representing this object
-
equals
-
hashCode
public int hashCode() -
toString
-