Java Utililty Methods Wildcard to Regex

List of utility methods to do Wildcard to Regex

Description

The list of methods to do Wildcard to Regex are organized into topic(s).

Method

Stringwildcard2Regex(final String wildcard)
wildcard Regex
StringBuffer s = new StringBuffer((int) (wildcard.length() * 1.3));
String w = wildcard;
for (int i = 0, is = w.length(); i < is; i++) {
    char c = w.charAt(i);
    switch (c) {
    case '*':
        s.append(".*");
        break;
...
Stringwildcard2regexp(String wildcard)
Compile wildcard to regexp
StringBuffer sb = new StringBuffer("^");
for (int i = 0; i < wildcard.length(); i++) {
    char c = wildcard.charAt(i);
    switch (c) {
    case '.':
        sb.append("\\.");
        break;
    case '*':
...
StringwildcardAsRegex(String patternWithWildcards)
converts a string containing wildcards (* and ?) into a valid regex
if (patternWithWildcards == null) {
    throw new IllegalArgumentException("patternWithWildcards must not be null");
return "\\Q" + patternWithWildcards.replace("?", "\\E.\\Q").replace("*", "\\E.*\\Q") + "\\E";
StringwildcardAsRegex(String wildcard)
convert a wild card containing * and ?
StringBuilder sb = new StringBuilder();
for (int i = 0; i < wildcard.length(); i++) {
    final char c = wildcard.charAt(i);
    switch (c) {
    case '*':
        sb.append(".*?");
        break;
    case '?':
...
StringwildcardToJavaRegex(String expr)
wildcard To Java Regex
if (expr == null) {
    throw new IllegalArgumentException("expr is null");
String regex = expr.replaceAll("([(){}\\[\\].+^$])", "\\\\$1"); 
regex = regex.replaceAll("\\*", ".*"); 
regex = regex.replaceAll("\\?", "."); 
return regex;
StringwildcardToJavaRegexp(String expr)
wildcard To Java Regexp
if (expr == null) {
    throw new IllegalArgumentException("expr is null");
String regex = expr.replaceAll("([(){}\\[\\].+^$])", "\\\\$1"); 
regex = regex.replaceAll("\\*", ".*"); 
regex = regex.replaceAll("\\?", "."); 
return regex;
StringwildcardToRegex(CharSequence s)
wildcard To Regex
StringBuilder sb = new StringBuilder();
int len = s.length();
for (int i = 0; i < len; i++) {
    char ch = s.charAt(i);
    switch (ch) {
    case '*':
        sb.append(".*");
        break;
...
StringwildcardToRegex(final String input)
wildcard To Regex
StringBuilder sb = new StringBuilder(input.length() + 10);
sb.append('^');
for (int i = 0; i < input.length(); ++i) {
    char c = input.charAt(i);
    if (c == '*') {
        sb.append(".*");
    } else if (c == '?') {
        sb.append('.');
...
StringwildcardToRegex(final String pattern)
wildcard To Regex
if (pattern == null) {
    return null;
return pattern.replace("*", ".*").replace("?", ".");
StringwildcardToRegex(String toSearch, boolean supportSQLWildcard)
Convert a string that is expected to have standard "filename wildcards" to a matching regular expression.
StringBuilder s = new StringBuilder(toSearch.length() + 5);
s.append('^');
for (int i = 0, is = toSearch.length(); i < is; i++) {
    char c = toSearch.charAt(i);
    if (c == '*' || (c == '%' && supportSQLWildcard)) 
        s.append(".*");
    } else if (c == '?') {
...