Java Wildcard Match wildcardMatch(String pattern, String string)

Here you can find the source of wildcardMatch(String pattern, String string)

Description

wildcard Match

License

Apache License

Declaration

public static boolean wildcardMatch(String pattern, String string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static boolean wildcardMatch(String pattern, String string) {
        int stringLength = string.length();
        int stringIndex = 0;
        for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) {
            char c = pattern.charAt(patternIndex);
            if (c == '*') {
                // Recurse with the pattern without this '*' and the actual
                // string, until
                // match is found or we inspected the whole string
                while (stringIndex < stringLength) {
                    if (wildcardMatch(pattern.substring(patternIndex + 1), string.substring(stringIndex))) {
                        return true;
                    }/*from  ww  w  .  jav a  2 s  .c  om*/
                    // No match found, try a shorter string, since we are
                    // matching '*'
                    ++stringIndex;
                }
            } else if (c == '?') {
                // Increment the string index, since '?' match a single char in
                // the string
                ++stringIndex;
                if (stringIndex > stringLength) {
                    return false;
                }
            } else {
                // A normal character in the pattern, must match the one in the
                // string
                if (stringIndex >= stringLength || c != string.charAt(stringIndex)) {
                    return false;
                }
                ++stringIndex;
            }
        }

        // I've inspected the whole pattern, but not the whole string
        return stringIndex == stringLength;
    }
}

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(final String text, final String wildcard)
  4. wildcardMatch(String input, String pattern)
  5. wildcardMatch(String pattern, String name)
  6. wildcardMatch(String string, String pattern)
  7. wildCardMatch(String text, String pattern)
  8. wildCardMatch(String text, String pattern)
  9. wildCardMatch(String text, String pattern)