Java Utililty Methods Regex Create

List of utility methods to do Regex Create

Description

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

Method

StringtoRegex(String includes)
to Regex
return includes == null ? ".*" : includes.replace(".", "/").replace("*", ".*");
StringtoRegEx(String input)
Convert the incoming string to a regular expression.
return input.replace(".", "\\.").replace("*", ".*");
StringtoRegex(String name)
Computes the regular expression corresponding to a package or class name, by escaping dots.
return name.replace(STRING_DOT, REGEX_DOT);
StringtoRegex(String param)
Convert the string into a regex to allow easy matching.
StringBuffer regex = new StringBuffer();
for (int i = 0; i < param.length(); i++) {
    char next = param.charAt(i);
    if ('*' == next)
        regex.append(".+"); 
    else if ('?' == next)
        regex.append("."); 
    else if ('.' == next)
...
StringtoRegex(String wildcard)
convert wild card to regular expression
String converted = "^" + wildcard.replaceAll("(^|/)\\*\\*/", "@@").replaceAll("(^|/)\\*\\*", "@@")
        .replaceAll("\\.", "\\\\.").replaceAll("\\*", "[^/]*").replaceAll("\\?", ".");
if (converted.contains("@@")) {
    converted = converted.replaceAll("@@", "(.*/)?") + "((^|/).*)?$";
return converted;
StringtoRegExFormat(String pattern)
Converts wildcard format ('*' and '?') to reg-ex format.
StringBuffer regex = new StringBuffer(pattern.length());
boolean escaped = false;
boolean quoting = false;
for (int i = 0; i < pattern.length(); i++) {
    char c = pattern.charAt(i);
    if (c == '*' && !escaped) {
        if (quoting) {
            regex.append("\\E");
...
StringtoRegexPattern(String filter)
Convert filter to a regular expression
StringBuffer pattern = new StringBuffer(".*"); 
boolean isWaitingForEndQuote = false;
for (int i = 0; i < filter.length(); i++) {
    char c = filter.charAt(i);
    if (c == '*' || c == '?') {
        if (isWaitingForEndQuote) {
            pattern.append("\\E"); 
            isWaitingForEndQuote = false;
...
StringtoRegexPattern(String pattern)
Converts a pattern string with '*' and '?'
int patternLength = pattern.length();
StringBuffer result = new StringBuffer(patternLength);
boolean escaped = false;
boolean quoting = false;
for (int i = 0; i < patternLength; i++) {
    char ch = pattern.charAt(i);
    switch (ch) {
    case '*': {
...