Java Wildcard Match wildcardMatch(String pattern, String name)

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

Description

Match pattern against name, where pattern may contain wildcards ('*').

License

Open Source License

Parameter

Parameter Description
pattern the pattern to match; may contain '*' which match any number (0 or more) of any character (think file globbing)
name the name to match against the pattern

Return

true if the name matches the pattern; false otherwise

Declaration

public static final boolean wildcardMatch(String pattern, String name) 

Method Source Code

//package com.java2s;
/*//from w  w w  .  j av  a2  s .c  om
 * @(#)Util.java             0.3-3 06/05/2001
 *
 *  This file is part of the HTTPClient package
 *  Copyright (C) 1996-2001 Ronald Tschal???r
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA 02111-1307, USA
 *
 *  For questions, suggestions, bug-reports, enhancement-requests etc.
 *  I may be contacted at:
 *
 *  ronald@innovation.ch
 *
 *  The HTTPClient's home page is located at:
 *
 *  http://www.innovation.ch/java/HTTPClient/ 
 *
 */

public class Main {
    /**
     * Match <var>pattern</var> against <var>name</var>, where <var>pattern</var>
     * may contain wildcards ('*').
     * @param pattern the pattern to match; may contain '*' which match any number
     *          (0 or more) of any character (think file globbing)
     * @param name the name to match against the pattern
     * @return true if the name matches the pattern; false otherwise
     */
    public static final boolean wildcardMatch(String pattern, String name) {
        return wildcardMatch(pattern, name, 0, 0, pattern.length(), name.length());
    }

    private static final boolean wildcardMatch(String pattern, String name, int ppos, int npos, int plen,
            int nlen) {
        // find wildcard
        int star = pattern.indexOf('*', ppos);
        if (star < 0) {
            return ((plen - ppos) == (nlen - npos) && pattern.regionMatches(ppos, name, npos, plen - ppos));
        }

        // match prefix
        if (!pattern.regionMatches(ppos, name, npos, star - ppos))
            return false;

        // match suffix
        if (star == plen - 1)
            return true;
        while (!wildcardMatch(pattern, name, star + 1, npos, plen, nlen) && npos < nlen)
            npos++;
        return (npos < nlen);
    }
}

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 string)
  6. wildcardMatch(String string, String pattern)
  7. wildCardMatch(String text, String pattern)
  8. wildCardMatch(String text, String pattern)