Here you can find the source of wildcardToRegex(String wildcard)
Parameter | Description |
---|---|
wildcard | the string |
private static String wildcardToRegex(String wildcard)
//package com.java2s; //License from project: Apache License public class Main { /**//from www .ja v a2s . co m * Converts given string containing wildcards (* or ?) to its corresponding regular expression. * * @param wildcard the string * @return the regular expression */ private 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()); } }