Regular Expression Wildcard Matching

前端 未结 9 1495
甜味超标
甜味超标 2020-12-15 04:13

I have a list of about 120 thousand english words (basically every word in the language).

I need a regular expression that would allow searching through these words

9条回答
  •  悲&欢浪女
    2020-12-15 05:03

    This is what I use:

    String wildcardToRegex(String wildcardString) {
        // The 12 is arbitrary, you may adjust it to fit your needs depending
        // on how many special characters you expect in a single pattern.
        StringBuilder sb = new StringBuilder(wildcardString.length() + 12);
        sb.append('^');
        for (int i = 0; i < wildcardString.length(); ++i) {
            char c = wildcardString.charAt(i);
            if (c == '*') {
                sb.append(".*");
            } else if (c == '?') {
                sb.append('.');
            } else if ("\\.[]{}()+-^$|".indexOf(c) >= 0) {
                sb.append('\\');
                sb.append(c);
            } else {
                sb.append(c);
            }
        }
        sb.append('$');
        return sb.toString();
    }
    

    Special character list from https://stackoverflow.com/a/26228852/1808989.

提交回复
热议问题