From wildcards to regular expressions
问题 I want to allow the two main wildcards ? and * to filter my data. Here is how I'm doing now (as I saw on many websites): public boolean contains(String data, String filter) { if(data == null || data.isEmpty()) { return false; } String regex = filter.replace(".", "[.]") .replace("?", ".") .replace("*", ".*"); return Pattern.matches(regex, data); } But shouldn't we escape all the other regex special chars, like | or ( , etc.? And also, maybe we could preserve ? and * if they are preceded by a \