Class StringReplace

  • All Implemented Interfaces:
    AdditionalInformationHandler, CleanUpHandler, Destroyable, GlobalInfoSupporter, LoggingLevelHandler, LoggingSupporter, OptionHandler, QuickInfoSupporter, ShallowCopySupporter<Actor>, SizeOfHandler, Stoppable, StoppableWithFeedback, VariablesInspectionHandler, VariableChangeListener, Actor, ErrorHandler, InputConsumer, OutputProducer, Serializable, Comparable

    public class StringReplace
    extends AbstractStringOperation
    Performs a string replacement, using either String.replaceFirst(...) or String.replaceAll(...). Special characters like \n \r \t and \ need to be escaped properly. The input is expected to be escaped, i.e., the string "\t" will get turned into the character '\t'.
    If the 'replace' string contains both, variables and placeholders, then first all variables are expanded and then the placeholders. This ensures that variables containing placeholders expand their placeholders as well. Not expanding placeholders will cause 'Illegal group reference' error messages.
    By enabling 'useDotAll', matching across lines is possible as well.
    If no regular expression matching is required, you can also use the simple replacing, which uses String.replace(...).

    Input/output:
    - accepts:
       java.lang.String
       java.lang.String[]
    - generates:
       java.lang.String
       java.lang.String[]


    -logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel)
        The logging level for outputting errors and debugging output.
        default: WARNING
        min-user-mode: Expert
     
    -name <java.lang.String> (property: name)
        The name of the actor.
        default: StringReplace
     
    -annotation <adams.core.base.BaseAnnotation> (property: annotations)
        The annotations to attach to this actor.
        default:
     
    -skip <boolean> (property: skip)
        If set to true, transformation is skipped and the input token is just forwarded
        as it is.
        default: false
     
    -stop-flow-on-error <boolean> (property: stopFlowOnError)
        If set to true, the flow execution at this level gets stopped in case this
        actor encounters an error; the error gets propagated; useful for critical
        actors.
        default: false
        min-user-mode: Expert
     
    -silent <boolean> (property: silent)
        If enabled, then no errors are output in the console; Note: the enclosing
        actor handler must have this enabled as well.
        default: false
        min-user-mode: Expert
     
    -find <adams.core.base.BaseRegExp> (property: find)
        The string to find (a regular expression).
        default: find
        more: https://docs.oracle.com/javase/tutorial/essential/regex/
        https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html
     
    -replace <java.lang.String> (property: replace)
        The string to replace the occurrences with.
        default:
     
    -type <FIRST|ALL|SIMPLE> (property: replaceType)
        Defines whether to use regular expression replacement (first/all) or simple
        string sequence replacement.
        default: FIRST
     
    -placeholder <boolean> (property: replaceContainsPlaceholder)
        Set this to true to enable automatic placeholder expansion for the replacement
        string.
        default: false
     
    -variable <boolean> (property: replaceContainsVariable)
        Set this to true to enable automatic variable expansion for the replacement
        string.
        default: false
     
    -use-dot-all <boolean> (property: useDotAll)
        If enabled, new-lines are treated as regular characters, enabling matching
        across multiple lines, see: https://docs.oracle.com/en/java/javase/11/docs
        /api/java.base/java/util/regex/Pattern.html#DOTALL
        default: false
     
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Detail

      • m_Find

        protected BaseRegExp m_Find
        the string to find.
      • m_Replace

        protected String m_Replace
        the replacement string.
      • m_ReplaceContainsPlaceholder

        protected boolean m_ReplaceContainsPlaceholder
        whether the replace string contains a placeholder, which needs to be expanded first.
      • m_ReplaceContainsVariable

        protected boolean m_ReplaceContainsVariable
        whether the replace string contains a variable, which needs to be expanded first.
      • m_UseDotAll

        protected boolean m_UseDotAll
        whether to use dot all.
      • m_Pattern

        protected transient Pattern m_Pattern
        the pattern.
    • Constructor Detail

      • StringReplace

        public StringReplace()
    • Method Detail

      • reset

        protected void reset()
        Resets the scheme.
        Overrides:
        reset in class AbstractActor
      • setFind

        public void setFind​(BaseRegExp value)
        Sets the string to find (regular expression).
        Parameters:
        value - the string
      • getFind

        public BaseRegExp getFind()
        Returns the string to find (regular expression).
        Returns:
        the string
      • findTipText

        public String findTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setReplace

        public void setReplace​(String value)
        Sets the string to replace the occurrences with.
        Parameters:
        value - the string
      • getReplace

        public String getReplace()
        Returns the string to replace the occurences with.
        Returns:
        the string
      • replaceTipText

        public String replaceTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setReplaceType

        public void setReplaceType​(StringReplace.ReplaceType value)
        Sets how to replace the strings.
        Parameters:
        value - the type
      • replaceTypeTipText

        public String replaceTypeTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setReplaceContainsPlaceholder

        public void setReplaceContainsPlaceholder​(boolean value)
        Sets whether the replace string contains a placeholder which needs to be expanded first.
        Parameters:
        value - true if replace string contains a placeholder
      • getReplaceContainsPlaceholder

        public boolean getReplaceContainsPlaceholder()
        Returns whether the replace string contains a placeholder which needs to be expanded first.
        Returns:
        true if replace string contains a placeholder
      • replaceContainsPlaceholderTipText

        public String replaceContainsPlaceholderTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setReplaceContainsVariable

        public void setReplaceContainsVariable​(boolean value)
        Sets whether the replace string contains a variable which needs to be expanded first.
        Parameters:
        value - true if replace string contains a variable
      • getReplaceContainsVariable

        public boolean getReplaceContainsVariable()
        Returns whether the replace string contains a variable which needs to be expanded first.
        Returns:
        true if replace string contains a variable
      • replaceContainsVariableTipText

        public String replaceContainsVariableTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setUseDotAll

        public void setUseDotAll​(boolean value)
        Sets whether to enabled matching across multiple lines.
        Parameters:
        value - true if to use dot all
      • getUseDotAll

        public boolean getUseDotAll()
        Returns whether to enabled matching across multiple lines.
        Returns:
        true if to use dot all
      • useDotAllTipText

        public String useDotAllTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • process

        protected String process​(String s,
                                 int index)
        Processes the string.
        Specified by:
        process in class AbstractStringOperation
        Parameters:
        s - the string to process
        index - the 0-based index of the string currently being processed
        Returns:
        the processed string or null if nothing produced