Java Wildcard Match wildCardMatch(String text, String pattern)

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

Description

Performs a wildcard comparison between two strings, where pattern contains the string with wildcards and textLicense Open Source License

Parameter

Parameter Description
text a parameter
pattern a parameter

Declaration

public static boolean wildCardMatch(String text, String pattern) 

Method Source Code

//package com.java2s;
/*//from ww w . jav  a2 s  .  c  o  m
 * This project is available under the BSD 2-Clause License.
 *
 * Copyright 2013 Will Preston <sigma@sigmatauproductions.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Project itself.
 */

public class Main {
    /**
     * Performs a wildcard comparison between two strings, where
     * <code>pattern</code> contains the string with wildcards and
     * <code>text</code contains the string to be searched.  Method originally
     * written by Adarsh Ramamurthy and can be found on his blog at the URL
     * http://www.adarshr.com/papers/wildcard
     *
        
     *
     * @param text
     * @param pattern
     * @return
     */
    public static boolean wildCardMatch(String text, String pattern) {
        // Create the cards by splitting using a RegEx. If more speed 
        // is desired, a simpler character based splitting can be done.
        String[] cards = pattern.split("\\*");

        // Iterate over the cards.
        for (String card : cards) {
            int idx = text.indexOf(card);

            // Card not detected in the text.
            if (idx == -1) {
                return false;
            }

            // Move ahead, towards the right of the text.
            text = text.substring(idx + card.length());
        }

        return true;
    }
}

Related

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