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 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
  • Constructor Details

    • 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 matching
      method - matching method
    • TextMatcher

      public TextMatcher(TextMatcher textMatcher)
      Copy constructor. Supplying null is the same as evoking the empty constructor.
      Parameters:
      textMatcher - instance to copy
  • Method Details

    • 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 with Predicate. Same as invoking matches(CharSequence).
      Specified by:
      test in interface Predicate<CharSequence>
      Parameters:
      text - text to match
      Returns:
      true if matching
    • matches

      public boolean matches(CharSequence text)
      Matches this class pattern against its text.
      Parameters:
      text - text to match
      Returns:
      true if matching
    • apply

      public String apply(String text, String replacement)
      Replaces this class matching text with replacement value. For compatibility with BiFunction. Same as invoking replace(String, String).
      Specified by:
      apply in interface BiFunction<String,String,String>
      Parameters:
      text - text to match
      replacement - 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 match
      replacement - text replacement
      Returns:
      replaced text
    • toRegexMatcher

      public Matcher toRegexMatcher(CharSequence text)
      Converts this text matcher to a pattern Matcher.
      Parameters:
      text - text to match using this text matcher method
      Returns:
      matcher
    • toRegexPattern

      public Pattern toRegexPattern()
      Compiles this text matcher to create a regular expression Pattern.
      Returns:
      pattern
    • loadFromXML

      public void loadFromXML(XML xml)
      Description copied from interface: IXMLConfigurable
      Load XML configuration values and initialized this object with them.
      Specified by:
      loadFromXML in interface IXMLConfigurable
      Parameters:
      xml - the XML to load into this object
    • saveToXML

      public void saveToXML(XML xml)
      Description copied from interface: IXMLConfigurable
      Saves this object as XML.
      Specified by:
      saveToXML in interface IXMLConfigurable
      Parameters:
      xml - the XML that will representing this object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object