Class TextMatcher
- java.lang.Object
-
- com.norconex.commons.lang.text.TextMatcher
-
- All Implemented Interfaces:
IXMLConfigurable,BiFunction<String,String,String>,BinaryOperator<String>,Predicate<CharSequence>
public class TextMatcher extends Object implements Predicate<CharSequence>, BinaryOperator<String>, IXMLConfigurable
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
nullexpressions will match everything, but replace nothing. when invokingmatches(CharSequence)orreplace(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 Modifier and Type Class Description static classTextMatcher.Method
-
Constructor Summary
Constructors Constructor Description TextMatcher()Creates 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringapply(String text, String replacement)Replaces this class matching text with replacement value.static TextMatcherbasic(String pattern)Creates a new text matcher initialized with basic matching.voidcopyFrom(TextMatcher tm)voidcopyTo(TextMatcher tm)static TextMatchercsv(String pattern)Creates a new text matcher initialized with comma-separated-value matching.booleanequals(Object other)TextMatcher.MethodgetMethod()StringgetPattern()inthashCode()TextMatcherignoreCase()TextMatcherignoreDiacritic()booleanisIgnoreCase()booleanisIgnoreDiacritic()booleanisPartial()booleanisReplaceAll()voidloadFromXML(XML xml)Load XML configuration values and initialized this object with them.booleanmatches(CharSequence text)Matches this class pattern against its text.TextMatcherpartial()static TextMatcherregex(String pattern)Creates a new text matcher initialized with regular expression matching.Stringreplace(String text, String replacement)Replaces this class matching text with replacement value.TextMatcherreplaceAll()voidsaveToXML(XML xml)Saves this object as XML.TextMatchersetIgnoreCase(boolean ignoreCase)TextMatchersetIgnoreDiacritic(boolean ignoreDiacritic)TextMatchersetMethod(TextMatcher.Method method)TextMatchersetPartial(boolean partial)TextMatchersetPattern(String pattern)TextMatchersetReplaceAll(boolean replaceAll)booleantest(CharSequence text)Matches this class pattern against its text.MatchertoRegexMatcher(CharSequence text)Converts this text matcher to a patternMatcher.PatterntoRegexPattern()Compiles this text matcher to create a regular expressionPattern.StringtoString()static TextMatcherwildcard(String pattern)Creates a new text matcher initialized with wildcard matching.TextMatcherwithIgnoreCase(boolean ignoreCase)TextMatcherwithIgnoreDiacritic(boolean ignoreDiacritic)TextMatcherwithMethod(TextMatcher.Method method)TextMatcherwithPartial(boolean partial)TextMatcherwithPattern(String pattern)TextMatcherwithReplaceAll(boolean replaceAll)-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.function.BiFunction
andThen
-
-
-
-
Constructor Detail
-
TextMatcher
public TextMatcher()
Creates a basic matcher.
-
TextMatcher
public TextMatcher(String pattern)
Creates a basic matcher with the given pattern. Default behavior will match the pattern exactly.- Parameters:
pattern- expression used for matching
-
TextMatcher
public TextMatcher(TextMatcher.Method method)
Creates a matcher with the specified method.- Parameters:
method- matching method
-
TextMatcher
public TextMatcher(String pattern, TextMatcher.Method method)
Creates a basic matcher with the given pattern.- Parameters:
pattern- expression used for matchingmethod- matching method
-
TextMatcher
public TextMatcher(TextMatcher textMatcher)
Copy constructor. Supplyingnullis the same as evoking the empty constructor.- Parameters:
textMatcher- instance to copy
-
-
Method Detail
-
getMethod
public TextMatcher.Method getMethod()
-
setMethod
public TextMatcher setMethod(TextMatcher.Method method)
-
withMethod
public TextMatcher withMethod(TextMatcher.Method method)
-
isPartial
public boolean isPartial()
-
setPartial
public TextMatcher setPartial(boolean partial)
-
partial
public TextMatcher partial()
-
withPartial
public TextMatcher withPartial(boolean partial)
-
getPattern
public String getPattern()
-
setPattern
public TextMatcher setPattern(String pattern)
-
withPattern
public TextMatcher withPattern(String pattern)
-
isIgnoreCase
public boolean isIgnoreCase()
-
setIgnoreCase
public TextMatcher setIgnoreCase(boolean ignoreCase)
-
ignoreCase
public TextMatcher ignoreCase()
-
withIgnoreCase
public TextMatcher withIgnoreCase(boolean ignoreCase)
-
isIgnoreDiacritic
public boolean isIgnoreDiacritic()
-
setIgnoreDiacritic
public TextMatcher setIgnoreDiacritic(boolean ignoreDiacritic)
-
ignoreDiacritic
public TextMatcher ignoreDiacritic()
-
withIgnoreDiacritic
public TextMatcher withIgnoreDiacritic(boolean ignoreDiacritic)
-
isReplaceAll
public boolean isReplaceAll()
-
setReplaceAll
public TextMatcher setReplaceAll(boolean replaceAll)
-
replaceAll
public TextMatcher replaceAll()
-
withReplaceAll
public TextMatcher withReplaceAll(boolean replaceAll)
-
copyTo
public void copyTo(TextMatcher tm)
-
copyFrom
public void copyFrom(TextMatcher tm)
-
basic
public static TextMatcher basic(String pattern)
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
public static TextMatcher csv(String pattern)
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
public static TextMatcher wildcard(String pattern)
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
public static TextMatcher regex(String pattern)
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
public boolean test(CharSequence text)
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
public boolean matches(CharSequence text)
Matches this class pattern against its text.- Parameters:
text- text to match- Returns:
trueif matching
-
apply
public String apply(String text, String replacement)
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
public String replace(String text, String replacement)
Replaces this class matching text with replacement value.- Parameters:
text- text to matchreplacement- text replacement- Returns:
- replaced text
-
toRegexMatcher
public Matcher toRegexMatcher(CharSequence text)
Converts this text matcher to a patternMatcher.- Parameters:
text- text to match using this text matcher method- Returns:
- matcher
-
toRegexPattern
public Pattern toRegexPattern()
Compiles this text matcher to create a regular expressionPattern.- Returns:
- pattern
-
loadFromXML
public void loadFromXML(XML xml)
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
public void saveToXML(XML xml)
Description copied from interface:IXMLConfigurableSaves this object as XML.- Specified by:
saveToXMLin interfaceIXMLConfigurable- Parameters:
xml- the XML that will representing this object
-
-