Java Wildcard Match wildcardMatches(String pattern, String text)

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

Description

Check if pattern string matches text string.

License

Apache License

Declaration

public static boolean wildcardMatches(String pattern, String text) 

Method Source Code

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

public class Main {
    /**//  ww  w.ja  v a 2s  .  c om
     * Check if pattern string matches text string.
     *
     * At the beginning of iteration i of main loop
     *
     * old[j] = true if pattern[0..j] matches text[0..i-1]
     *
     * By comparing pattern[j] with text[i], the main loop computes
     *
     * states[j] = true if pattern[0..j] matches text[0..i]
     *
     */
    public static boolean wildcardMatches(String pattern, String text) {
        // add sentinel so don't need to worry about *'s at end of pattern
        text += '\0';
        pattern += '\0';

        int N = pattern.length();

        boolean[] states = new boolean[N + 1];
        boolean[] old = new boolean[N + 1];
        old[0] = true;

        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            states = new boolean[N + 1]; // initialized to false
            for (int j = 0; j < N; j++) {
                char p = pattern.charAt(j);

                // hack to handle *'s that match 0 characters
                if (old[j] && (p == '*'))
                    old[j + 1] = true;

                if (old[j] && (p == c))
                    states[j + 1] = true;
                if (old[j] && (p == '.'))
                    states[j + 1] = true;
                if (old[j] && (p == '*'))
                    states[j] = true;
                if (old[j] && (p == '*'))
                    states[j + 1] = true;
            }
            old = states;
        }
        return states[N];
    }
}

Related

  1. wildCardMatch(String text, String pattern)
  2. wildCardMatch(String text, String pattern)
  3. wildCardMatch(String text, String pattern)
  4. wildCardMatch(String text, String pattern)
  5. wildCardMatch(String text, String wildcard)