Package ij.util

Class WildcardMatch


  • public class WildcardMatch
    extends Object
    This class allows for simple wildcard pattern matching. Possible patterns allow to match single characters ('?') or any count of characters ('*').

    Wildcard characters can be escaped (default: by an '\').

    This class always matches for the whole word.

    Examples:

     WildcardMatch wm = new WildcardMatch();
     System.out.println(wm.match("CfgOptions.class", "C*.class"));    // true
     System.out.println(wm.match("CfgOptions.class", "?gOpti*c?as?"));  // false
     System.out.println(wm.match("CfgOptions.class", "??gOpti*c?ass")); // true
     System.out.println(wm.match("What's this?",       "What*\\?"));          // true
     System.out.println(wm.match("What's this?",       "What*?"));            // true
     System.out.println(wm.match("A \\ backslash", "*\\\\?back*"));   // true
     
    • Constructor Detail

      • WildcardMatch

        public WildcardMatch()
      • WildcardMatch

        public WildcardMatch​(char singleChar,
                             char multipleChars)
    • Method Detail

      • setWildcardChars

        public void setWildcardChars​(char singleChar,
                                     char multipleChars)
        Sets new characters to be used as wildcard characters, overriding the the default of '?' for any single character match and '*' for any amount of characters, including 0 characters.
        Parameters:
        singleChar - The char used to match exactly ONE character.
        multipleChars - The char used to match any amount of characters including o characters.
      • setEscapeChar

        public void setEscapeChar​(char escapeChar)
        Sets the new character to be used as an escape character, overriding the the default of '\'.
        Parameters:
        escapeChar - The char used to match escape wildcard characters.
      • getSingleWildcardChar

        public char getSingleWildcardChar()
        Returns the character used to specify exactly one character.
        Returns:
        Wildcard character matching any single character.
      • getMultipleWildcardChar

        public char getMultipleWildcardChar()
        Returns the character used to specify any amount of characters.
        Returns:
        Wildcard character matching any count of characters.
      • getEscapeChar

        public char getEscapeChar()
        Returns the character used to escape the wildcard functionality of a wildcard character. If two escape characters are used in sequence, they mean the escape character itself. It defaults to '\'.
        Returns:
        Escape character.
      • setCaseSensitive

        public void setCaseSensitive​(boolean caseSensitive)
        Makes pattern matching case insensitive.
        Parameters:
        caseSensitive - false for case insensitivity. Default is case sensitive match.
      • getCaseSensitive

        public boolean getCaseSensitive()
        Returns the current state of case sensitivity.
        Returns:
        true for case sensitive pattern matching, false otherwise.
      • match

        public boolean match​(String s,
                             String pattern)
        Matches a string against a pattern with wildcards. Two wildcard types are supported: single character match (defaults to '?') and ANY character match ('*'), matching any count of characters including 0. Wildcard characters may be escaped by an escape character, which defaults to '\'.
        Parameters:
        s - The string, in which the search should be performed.
        pattern - The search pattern string including wildcards.
        Returns:
        true, if string 's' matches 'pattern'.