Java Wildcard Match wildcardMatch(String string, String pattern)

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

Description

wildcard Match

License

Apache License

Declaration

public static boolean wildcardMatch(String string, String pattern) 

Method Source Code

//package com.java2s;
/**//from w ww. j av a  2  s. c  o  m
 *
 * dry-redis: In-memory pure java implementation to Redis
 * Copyright (c) 2016, Sandeep Gupta
 * 
 * http://sangupta.com/projects/dry-redis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

public class Main {
    public static boolean wildcardMatch(String string, String pattern) {
        int i = 0;
        int j = 0;
        int starIndex = -1;
        int iIndex = -1;

        while (i < string.length()) {
            if (j < pattern.length()
                    && (pattern.charAt(j) == '?' || pattern.charAt(j) == string
                            .charAt(i))) {
                ++i;
                ++j;
            } else if (j < pattern.length() && pattern.charAt(j) == '*') {
                starIndex = j;
                iIndex = i;
                j++;
            } else if (starIndex != -1) {
                j = starIndex + 1;
                i = iIndex + 1;
                iIndex++;
            } else {
                return false;
            }
        }

        while (j < pattern.length() && pattern.charAt(j) == '*') {
            ++j;
        }

        return j == pattern.length();
    }
}

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