Java Wildcard Match wildCardMatch(String text, String pattern)

Here you can find the source of wildCardMatch(String text, String pattern)

Description

Returns if the given text matches the given search expression.

License

Open Source License

Parameter

Parameter Description
text the text to check
pattern the pattern to match with or without wildcards, for example 'x*y*z'

Declaration

public static boolean wildCardMatch(String text, String pattern) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w  ww. ja  va2 s . co  m
     * Returns if the given text matches the given search expression.
     * @param text the text to check
     * @param pattern the pattern to match with or without wildcards, for example 'x*y*z'
     */
    public static boolean wildCardMatch(String text, String pattern) {
        if (pattern == null || pattern.length() == 0) {
            return true;
        }
        if (pattern.length() == 1 && pattern.charAt(0) == '*') {
            return true;
        }
        if (text == null || text.length() == 0) {
            return false;
        }

        int patternIdx = 0;
        int txtIdx = 0;
        StringBuilder matchTag = new StringBuilder();
        boolean isFirst = true;
        while (true) {
            matchTag.setLength(0);
            patternIdx = nextMatchTag(pattern, patternIdx, matchTag);
            if (matchTag.length() == 0) {
                // Wildcard...
                if (patternIdx == -1) {
                    return true;
                }
                isFirst = false;
                continue;
            }
            String matchTagString = matchTag.toString();
            if (isFirst && patternIdx == -1) {
                // First pattern equals last pattern <> *, txt must be equals:
                return text.equals(matchTagString);
            } else if (isFirst) {
                // First pattern part <> *, txt must start with matchTag:
                if (!text.startsWith(matchTagString)) {
                    return false;
                }
                txtIdx = matchTag.length();
            } else if (patternIdx == -1) {
                // Last pattern part <> *, txt must end with matchTag:
                return text.endsWith(matchTagString);
            } else {
                // Pattern in between, txt must contain it:
                txtIdx = text.indexOf(matchTagString, txtIdx);
                if (txtIdx == -1) {
                    return false;
                }
            }
            isFirst = false;
        }
    }

    private static int nextMatchTag(String pattern, int fromIdx, StringBuilder tag) {
        for (int i = fromIdx; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (c == '*') {
                return i + 1;
            } else {
                tag.append(c);
            }
        }
        return -1;
    }
}

Related

  1. wildcardMatch(String input, String pattern)
  2. wildcardMatch(String pattern, String name)
  3. wildcardMatch(String pattern, String string)
  4. wildcardMatch(String string, String pattern)
  5. wildCardMatch(String text, String pattern)
  6. wildCardMatch(String text, String pattern)
  7. wildCardMatch(String text, String pattern)
  8. wildCardMatch(String text, String wildcard)
  9. wildcardMatches(String pattern, String text)