Pattern (String text) { StringBuilder builder = new StringBuilder(text.length()); for (int i = 0; i < text.length(); i++) { final char c = text.charAt(i); if (c == ' ' || Character.isLetter(c) || Character.isDigit(c) || c == '_') { builder.append(c); } else if (c == '*') { builder.append(".*"); } else { builder.append('\\').append(c); } } return Pattern.compile(builder.toString()); }