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:
Match/replace methods are case-sensitive by default.
This class is not thread-safe.
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.
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]"
<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.".
Modifier and Type | Class and Description |
---|---|
static class |
TextMatcher.Method |
Constructor and Description |
---|
TextMatcher()
Creates a basic matcher.
|
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.
|
TextMatcher(TextMatcher.Method method)
Creates a matcher with the specified method.
|
TextMatcher(TextMatcher textMatcher)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
String |
apply(String text,
String replacement)
Replaces this class matching text with replacement value.
|
static TextMatcher |
basic(String pattern)
Creates a new text matcher initialized with basic matching.
|
void |
copyFrom(TextMatcher tm) |
void |
copyTo(TextMatcher tm) |
static TextMatcher |
csv(String pattern)
Creates a new text matcher initialized with comma-separated-value
matching.
|
boolean |
equals(Object other) |
TextMatcher.Method |
getMethod() |
String |
getPattern() |
int |
hashCode() |
TextMatcher |
ignoreCase() |
TextMatcher |
ignoreDiacritic() |
boolean |
isIgnoreCase() |
boolean |
isIgnoreDiacritic() |
boolean |
isPartial() |
boolean |
isReplaceAll() |
void |
loadFromXML(XML xml)
Load XML configuration values and initialized this object with them.
|
boolean |
matches(CharSequence text)
Matches this class pattern against its text.
|
TextMatcher |
partial() |
static TextMatcher |
regex(String pattern)
Creates a new text matcher initialized with regular expression
matching.
|
String |
replace(String text,
String replacement)
Replaces this class matching text with replacement value.
|
TextMatcher |
replaceAll() |
void |
saveToXML(XML xml)
Saves this object as XML.
|
TextMatcher |
setIgnoreCase(boolean ignoreCase) |
TextMatcher |
setIgnoreDiacritic(boolean ignoreDiacritic) |
TextMatcher |
setMethod(TextMatcher.Method method) |
TextMatcher |
setPartial(boolean partial) |
TextMatcher |
setPattern(String pattern) |
TextMatcher |
setReplaceAll(boolean replaceAll) |
boolean |
test(CharSequence text)
Matches this class pattern against its text.
|
Matcher |
toRegexMatcher(CharSequence text)
Converts this text matcher to a pattern
Matcher . |
Pattern |
toRegexPattern()
Compiles this text matcher to create a regular expression
Pattern . |
String |
toString() |
static TextMatcher |
wildcard(String pattern)
Creates a new text matcher initialized with wildcard matching.
|
TextMatcher |
withIgnoreCase(boolean ignoreCase) |
TextMatcher |
withIgnoreDiacritic(boolean ignoreDiacritic) |
TextMatcher |
withMethod(TextMatcher.Method method) |
TextMatcher |
withPartial(boolean partial) |
TextMatcher |
withPattern(String pattern) |
TextMatcher |
withReplaceAll(boolean replaceAll) |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
maxBy, minBy
andThen
public TextMatcher()
public TextMatcher(String pattern)
pattern
- expression used for matchingpublic TextMatcher(TextMatcher.Method method)
method
- matching methodpublic TextMatcher(String pattern, TextMatcher.Method method)
pattern
- expression used for matchingmethod
- matching methodpublic TextMatcher(TextMatcher textMatcher)
null
is the same
as evoking the empty constructor.textMatcher
- instance to copypublic TextMatcher.Method getMethod()
public TextMatcher setMethod(TextMatcher.Method method)
public TextMatcher withMethod(TextMatcher.Method method)
public boolean isPartial()
public TextMatcher setPartial(boolean partial)
public TextMatcher partial()
public TextMatcher withPartial(boolean partial)
public String getPattern()
public TextMatcher setPattern(String pattern)
public TextMatcher withPattern(String pattern)
public boolean isIgnoreCase()
public TextMatcher setIgnoreCase(boolean ignoreCase)
public TextMatcher ignoreCase()
public TextMatcher withIgnoreCase(boolean ignoreCase)
public boolean isIgnoreDiacritic()
public TextMatcher setIgnoreDiacritic(boolean ignoreDiacritic)
public TextMatcher ignoreDiacritic()
public TextMatcher withIgnoreDiacritic(boolean ignoreDiacritic)
public boolean isReplaceAll()
public TextMatcher setReplaceAll(boolean replaceAll)
public TextMatcher replaceAll()
public TextMatcher withReplaceAll(boolean replaceAll)
public void copyTo(TextMatcher tm)
public void copyFrom(TextMatcher tm)
public static TextMatcher basic(String pattern)
Creates a new text matcher initialized with basic matching.
Same as invoking
new TextMatcher(Method.BASIC).setPattern(pattern)
.
pattern
- expression to match against valuespublic 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)
.
pattern
- expression to match against valuespublic static TextMatcher wildcard(String pattern)
Creates a new text matcher initialized with wildcard matching.
Same as invoking
new TextMatcher(Method.WILDCARD).setPattern(pattern)
.
pattern
- expression to match against valuespublic static TextMatcher regex(String pattern)
Creates a new text matcher initialized with regular expression
matching. Same as invoking
new TextMatcher(Method.REGEX).setPattern(pattern)
.
pattern
- expression to match against valuespublic boolean test(CharSequence text)
Predicate
. Same as invoking
matches(CharSequence)
.test
in interface Predicate<CharSequence>
text
- text to matchtrue
if matchingpublic boolean matches(CharSequence text)
text
- text to matchtrue
if matchingpublic String apply(String text, String replacement)
BiFunction
. Same as invoking
replace(String, String)
.apply
in interface BiFunction<String,String,String>
text
- text to matchreplacement
- text replacementpublic String replace(String text, String replacement)
text
- text to matchreplacement
- text replacementpublic Matcher toRegexMatcher(CharSequence text)
Matcher
.text
- text to match using this text matcher methodpublic Pattern toRegexPattern()
Pattern
.public void loadFromXML(XML xml)
IXMLConfigurable
loadFromXML
in interface IXMLConfigurable
xml
- the XML to load into this objectpublic void saveToXML(XML xml)
IXMLConfigurable
saveToXML
in interface IXMLConfigurable
xml
- the XML that will representing this objectCopyright © 2008–2023 Norconex Inc.. All rights reserved.