Java Wildcard to Regex wildcard2Regex(final String wildcard)

Here you can find the source of wildcard2Regex(final String wildcard)

Description

wildcard Regex

License

Apache License

Declaration

static final public String wildcard2Regex(final String wildcard) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    static final public String wildcard2Regex(final String wildcard) {
        StringBuffer s = new StringBuffer((int) (wildcard.length() * 1.3));
        String w = wildcard;/*from   w  w  w  .ja va 2 s.c o  m*/
        for (int i = 0, is = w.length(); i < is; i++) {
            char c = w.charAt(i);
            switch (c) {
            case '*':
                s.append(".*");
                break;
            case '?':
                s.append(".");
                break;
            case '(':
            case ')':
            case '[':
            case ']':
            case '$':
            case '^':
            case '.':
            case '{':
            case '}':
            case '|':
            case '\\':
                s.append("\\");
                s.append(c);
                break;
            default:
                s.append(c);
                break;
            }
        }
        return s.toString();
    }
}

Related

  1. wildcard2regexp(String wildcard)
  2. wildcardAsRegex(String patternWithWildcards)
  3. wildcardAsRegex(String wildcard)
  4. wildcardToJavaRegex(String expr)