Java Wildcard to Regex wildcardToRegex(String toSearch, boolean supportSQLWildcard)

Here you can find the source of wildcardToRegex(String toSearch, boolean supportSQLWildcard)

Description

Convert a string that is expected to have standard "filename wildcards" to a matching regular expression.

License

Apache License

Parameter

Parameter Description
toSearch the search expression
supportSQLWildcard if true, % is also recognized as a wildcard character

Return

a pattern that can be used as a regular expression

Declaration

public static String wildcardToRegex(String toSearch, boolean supportSQLWildcard) 

Method Source Code

//package com.java2s;
/*//from   www.j a va  2 s . co  m
 * StringUtil.java
 *
 * This file is part of SQL Workbench/J, http://www.sql-workbench.net
 *
 * Copyright 2002-2017, Thomas Kellerer
 *
 * Licensed under a modified Apache License, Version 2.0
 * that restricts the use for certain governments.
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at.
 *
 *     http://sql-workbench.net/manual/license.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * To contact the author please send an email to: support@sql-workbench.net
 *
 */

public class Main {
    public static final String REGEX_SPECIAL_CHARS = "\\[](){}.*+?$^|";

    /**
     * Convert a string that is expected to have standard "filename wildcards" to
     * a matching regular expression.
     *
     * <tt>*</tt> and <tt>%</tt> are treated the same.
     * For single character wildcards only a question mark is used.
     * The SQL single character wildcard (<tt>_</tt>) is not supported (even when supportSQLWildcard is true)
     *
     * @param toSearch            the search expression
     * @param supportSQLWildcard  if true, % is also recognized as a wildcard character
     *
     * @return a pattern that can be used as a regular expression
     */
    public static String wildcardToRegex(String toSearch, boolean supportSQLWildcard) {
        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)) // support filesystem wildcards and SQL wildcards
            {
                s.append(".*");
            } else if (c == '?') {
                s.append(".");
            } else {
                if (REGEX_SPECIAL_CHARS.indexOf(c) != -1) {
                    s.append('\\');
                }
                s.append(c);
            }
        }
        s.append('$');
        return s.toString();
    }

    public static int indexOf(CharSequence value, char c) {
        return indexOf(value, c, 1);
    }

    public static int indexOf(CharSequence value, char c, int occurance) {
        if (value == null)
            return -1;
        if (occurance <= 0)
            occurance = 1;
        int numFound = 0;

        for (int i = 0; i < value.length(); i++) {
            if (value.charAt(i) == c) {
                numFound++;
                if (numFound == occurance)
                    return i;
            }
        }
        return -1;
    }
}

Related

  1. wildcardToJavaRegex(String expr)
  2. wildcardToJavaRegexp(String expr)
  3. wildcardToRegex(CharSequence s)
  4. wildcardToRegex(final String input)
  5. wildcardToRegex(final String pattern)
  6. wildcardToRegex(String wildcard)
  7. wildcardToRegex(String wildcard)
  8. wildcardToRegex(String wildcardPattern)
  9. wildcardToRegexp(String globExp)