Class StrReplace

All Implemented Interfaces:
BoolCellProcessor, CellProcessor, DateCellProcessor, DoubleCellProcessor, LongCellProcessor, StringCellProcessor

Replaces each substring of the input string that matches the given regular expression with the given replacement. The regular expression pattern is compiled once then reused for efficiency.
  • Field Details

    • regexPattern

      private final Pattern regexPattern
    • replacement

      private final String replacement
  • Constructor Details

    • StrReplace

      public StrReplace(String regex, String replacement)
      Constructs a new StrReplace processor, which replaces each substring of the input that matches the regex with the supplied replacement.
      Parameters:
      regex - the regular expression to match
      replacement - the string to be substituted for each match
      Throws:
      IllegalArgumentException - if regex is empty
      NullPointerException - if regex or replacement is null
      PatternSyntaxException - if regex is not a valid regular expression
    • StrReplace

      public StrReplace(String regex, String replacement, StringCellProcessor next)
      Constructs a new StrReplace processor, which replaces each substring of the input that matches the regex with the supplied replacement, then calls the next processor in the chain.
      Parameters:
      regex - the regular expression to match
      replacement - the string to be substituted for each match
      next - the next processor in the chain
      Throws:
      IllegalArgumentException - if regex is empty
      NullPointerException - if regex or replacement is null
      PatternSyntaxException - if regex is not a valid regular expression
  • Method Details

    • checkPreconditions

      private static void checkPreconditions(String regex, String replacement)
      Checks the preconditions for creating a new StrRegExReplace processor.
      Parameters:
      regex - the supplied regular expression
      replacement - the supplied replacement text
      Throws:
      IllegalArgumentException - if regex is empty
      NullPointerException - if regex or replacement is null
    • execute

      public Object execute(Object value, CsvContext context)
      This method is invoked by the framework when the processor needs to process data or check constraints.
      Specified by:
      execute in interface CellProcessor
      Parameters:
      value - the value to be processed
      context - the CSV context
      Returns:
      the result of cell processor execution
      Throws:
      SuperCsvCellProcessorException - if value is null