Java Wildcard Match wildCardMatch(final String needle, final String haystack)

Here you can find the source of wildCardMatch(final String needle, final String haystack)

Description

Checks to see if needle matches haystack, where needle uses special characters known as wildcards.

License

Open Source License

Parameter

Parameter Description
needle a parameter
haystack a parameter

Return

TRUE if the needle matches the haystack, FALSE if otherwise.

Declaration

public static final boolean wildCardMatch(final String needle, final String haystack) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w ww.  j  a va 2 s.  c  o  m
     * Checks to see if needle matches haystack, where needle uses special characters known as wildcards.
     * Asterisks (*) and Question marks (?) have special meaning in wildcard-based strings: Asterisks are
     * meant to represent every character for any amount of length, even zero. Question marks represent every
     * character for only the length of one. That is to say:<br>
     * <br>
     * If you had <tt>*</tt> it would match everything, no matter what.<br>
     * If you had <tt>hel??</tt> or <tt>hel*</tt> it would match "<tt>hello</tt>", as well as "<tt>help!</tt>", though the latter
     * would match "<tt>hello there how are you doing?</tt>" and the former would not.<br>
     * <br>
     * @param needle
     * @param haystack
     * @return TRUE if the needle matches the haystack, FALSE if otherwise.
     */
    public static final boolean wildCardMatch(final String needle, final String haystack) {
        char[] wildcard = needle.toCharArray(), string = haystack.toCharArray();

        int str = 0, wil = 0;

        //      if (DEBUG)
        //      {
        //         System.out.println("\r\n");
        //         System.out.println("Wildcard: " + needle);
        //         System.out.println("String:   " + haystack);
        //      }

        for (; wil < wildcard.length; wil++) {
            char wild = wildcard[wil];

            //         if (DEBUG)
            //         {
            //            System.out.println("Wildcard char: " + wild);
            //            System.out.println("String char:   " + string[str]);
            //         }

            if (wild == '\\') {
                //            if (DEBUG)
                //               System.out.println("Encountered Escape character, skipping next char...");

                wil++;

                if (wildcard[wil] != string[str])
                    return false;

                str++;
            } else if (wild == '?') {
                str++;
            } else if (wild == '*') {
                //            if (DEBUG)
                //               System.out.println("Wildcard found");

                for (; ((wil + 1) < wildcard.length
                        && (wildcard[wil + 1] == '*' || wildcard[wil + 1] == '?')); wil++)
                    ;

                if ((wil + 1) < wildcard.length) {
                    if ((str == 0) && (string[str] == wildcard[wil + 1])) {
                        str++;
                        wil++;
                        continue;
                    }

                    str++;

                    //               if (DEBUG)
                    //                  System.out.print("Cycling through characters until we find '" + wildcard[wil+1] + "'... ");

                    //               if (DEBUG)
                    //               {
                    //                  while ( (string[str++] != wildcard[wil+1]) && (str < string.length) )
                    //                  {
                    //                     System.out.print(string[str]);
                    //                  }
                    //               }
                    //               else
                    //               {
                    while ((string[str++] != wildcard[wil + 1]) && (str < string.length))
                        ;
                    //               }

                    //               if (DEBUG)
                    //                  System.out.println();

                    if (str == string.length) {
                        //                  if (DEBUG)
                        //                     System.out.println("str == string.length, therefore breaking out to TRUE");
                        break;
                    }

                    wil++;
                    str--;
                } else {
                    // The wildcard is at the end of the string, therefore we just have to say everything matched ;) ;)
                    str = string.length;
                    break;
                }
                str++;
            } else {
                if (wild != string[str])
                    return false;

                str++;
            }
        }

        if (str != string.length)
            return false;

        return true;
    }
}

Related

  1. wildcardMatch(final String pattern, final int patternIndex, final String value, final int valueIndex)
  2. wildcardMatch(final String text, final String wildcard)
  3. wildcardMatch(String input, String pattern)
  4. wildcardMatch(String pattern, String name)