Java Wildcard Match wildcardMatch(final String text, final String wildcard)

Here you can find the source of wildcardMatch(final String text, final String wildcard)

Description

Return true if text matches with entered wildcard pattern.

License

Open Source License

Parameter

Parameter Description
text String under investigation text
wildcard String wildcard for text check

Return

true if text match with pattern

Declaration

public static boolean wildcardMatch(final String text, final String wildcard) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   ww  w . j  av a 2  s.  c  o m
     * Wildcard for any symbol ?.
     */
    public static final String ANY_SYMBOL = "\\u003f";
    /**
     * Wildcard for any set of symbols *.
     */
    public static final String ANY_SYMBOLS_SET = "\\u002A";
    /**
     * Wildcard for any set of dot symbol.
     */
    public static final String ANY_DOT_SYMBOLS = "\\.";
    /**
     * Wildcard for any symbol replacement for regular expression check.
     */
    private static final String ANY_SYMBOL_REGEXP_REPLACEMENT = "[\\\\w\\\\s-\\.]";
    /**
     * Wildcard for any set of symbols for regular expression check.
     */
    private static final String ANY_SYMBOLS_SET_REGEXP_REPLACEMENT = "[\\\\w\\\\s-\\.]*";
    /**
     * Wildcard for any set of symbols for regular expression check.
     */
    private static final String ANY_DOT_SYMBOL_REGEXP_REPLACEMENT = "\\\\.";

    /**
     * Return true if text matches with entered wildcard pattern.
     * Symbols that filters by wildcard is characters, numbers, underline symbol, dot symbol, space symbol and hyphen symbol.
     *
     * @param text
     *       {@link String} under investigation text
     * @param wildcard
     *       {@link String} wildcard for text check
     * @return true if text match with pattern
     */
    public static boolean wildcardMatch(final String text, final String wildcard) {
        if (text == null && wildcard == null)
            return true;

        String wildcardRegex = wildcard;
        if (wildcard != null) {
            wildcardRegex = wildcardRegex.replaceAll(ANY_SYMBOL, ANY_SYMBOL_REGEXP_REPLACEMENT);
            wildcardRegex = wildcardRegex.replaceAll(ANY_DOT_SYMBOLS, ANY_DOT_SYMBOL_REGEXP_REPLACEMENT);
            wildcardRegex = wildcardRegex.replaceAll(ANY_SYMBOLS_SET, ANY_SYMBOLS_SET_REGEXP_REPLACEMENT);
        }

        return regexMatch(text, wildcardRegex);
    }

    /**
     * Return true if text matches with entered regexp pattern.
     *
     * @param text
     *       {@link String} under investigation text
     * @param reqex
     *       {@link String} reqex for text check
     * @return true if text match with pattern
     */
    public static boolean regexMatch(final String text, final String reqex) {
        if (text == null && reqex == null)
            return true;

        return text != null && reqex != null && text.matches(reqex);
    }
}

Related

  1. wildCardMatch(final String needle, final String haystack)
  2. wildcardMatch(final String pattern, final int patternIndex, final String value, final int valueIndex)
  3. wildcardMatch(String input, String pattern)
  4. wildcardMatch(String pattern, String name)
  5. wildcardMatch(String pattern, String string)
  6. wildcardMatch(String string, String pattern)