Class RegexReplace


  • public class RegexReplace
    extends Object
    Search and Replace by Regular Expressions
    • Field Detail

      • pattern

        protected Pattern pattern
        Compiled Pattern
      • search

        protected String search
        Search
      • replace

        protected String replace
        Replace
    • Constructor Detail

      • RegexReplace

        public RegexReplace()
        Constructor
      • RegexReplace

        public RegexReplace​(String search,
                            String replace,
                            boolean compilePattern)
                     throws PatternSyntaxException
        Constructor
        Parameters:
        search - Regex Search Pattern
        replace - Replacement String
        compilePattern - True if pattern should be compiled, false otherwise
        Throws:
        PatternSyntaxException
    • Method Detail

      • getSearch

        public String getSearch()
        Returns the Search-String
        Returns:
        Search-String
      • setSearch

        public void setSearch​(String search,
                              boolean compilePattern)
                       throws PatternSyntaxException
        Sets the Search-String and try to compile the Pattern. Note: If compilePattern is false, then the pattern Member is set to null.
        Parameters:
        search - Search-String
        compilePattern - True if pattern should be compiled, false otherwise
        Throws:
        PatternSyntaxException
      • getReplace

        public String getReplace()
        Returns the Replace-String
        Returns:
        Replace-String
      • setReplace

        public void setReplace​(String replace)
        Sets the Replace-String
        Parameters:
        replace - Replace-String
      • executionNeeded

        protected boolean executionNeeded​(String input)
        Checks if execution of search or replace is possible and needed
        Parameters:
        input - Input
        Returns:
        True if execution of search or replace is possible and needed, false otherwise
      • executionNeeded

        protected boolean executionNeeded​(String input,
                                          int start)
        Checks if execution of search or replace is possible and needed
        Parameters:
        input - Input
        start - Start Index
        Returns:
        True if execution of search or replace is possible and needed, false otherwise
      • doSearch

        public int doSearch​(String input,
                            int start)
        Search-Method Returns the start-position when found the pattern Returns -1 if not found the pattern or an error occured
        Parameters:
        input - Input
        start - Startposition
        Returns:
        Found position
      • doReplace

        public String doReplace​(String input)
        Replace-Method which returns the replaced input String or the input if the pattern is not compiled, the input is empty or the replacement failed
        Parameters:
        input - Input
        Returns:
        Replaced String
      • doReplaceMatchRegion

        public String doReplaceMatchRegion​(String input,
                                           int start)
        Replace-Method which only returns the match region or an empty Stirng if the pattern is not compiled, start parameter is invalid (negative or higher than input length) or the input is empty
        Parameters:
        input - Input
        start - Startposition
        Returns:
        Replaced String