Share this page 

Convert wildcard to a regex expressionTag(s): IO


import java.util.regex.Pattern;

public class WildcardRegex {
    public WildcardRegex() {  }
    public static void main(String[] args) {
        String test = "123ABC";
        System.out.println(test);
        System.out.println(Pattern.matches(wildcardToRegex("1*"), test));
        System.out.println(Pattern.matches(wildcardToRegex("?2*"), test));
        System.out.println(Pattern.matches(wildcardToRegex("??2*"), test));
        System.out.println(Pattern.matches(wildcardToRegex("*A*"), test));
        System.out.println(Pattern.matches(wildcardToRegex("*Z*"), test));
        System.out.println(Pattern.matches(wildcardToRegex("123*"), test));
        System.out.println(Pattern.matches(wildcardToRegex("123"), test));
        System.out.println(Pattern.matches(wildcardToRegex("*ABC"), test));
        System.out.println(Pattern.matches(wildcardToRegex("*abc"), test));
        System.out.println(Pattern.matches(wildcardToRegex("ABC*"), test));
        /*
           output :
           123ABC
            true
            true
            false
            true
            false
            true
            false
            true
            false
            false
        */
        
    }
    
    public static String wildcardToRegex(String wildcard){
        StringBuffer s = new StringBuffer(wildcard.length());
        s.append('^');
        for (int i = 0, is = wildcard.length(); i < is; i++) {
            char c = wildcard.charAt(i);
            switch(c) {
                case '*':
                    s.append(".*");
                    break;
                case '?':
                    s.append(".");
                    break;
                    // escape special regexp-characters
                case '(': case ')': case '[': case ']': case '$':
                case '^': case '.': case '{': case '}': case '|':
                case '\\':
                    s.append("\\");
                    s.append(c);
                    break;
                default:
                    s.append(c);
                    break;
            }
        }
        s.append('$');
        return(s.toString());
    }
}