Java Wildcard Match wildcardMatch(final String pattern, final int patternIndex, final String value, final int valueIndex)

Here you can find the source of wildcardMatch(final String pattern, final int patternIndex, final String value, final int valueIndex)

Description

Performs a match operation using wildcards.

License

Apache License

Parameter

Parameter Description
patternIndex the patternIndex
tmpIndex the current index
value the value
valueIndex the current value

Return

true on match

Declaration


private static boolean wildcardMatch(final String pattern, final int patternIndex, final String value,
        final int valueIndex) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//ww w  .j  a  va2  s  . com
     * The wildcard for a single character (?)
     */
    public static final char SINGLE_TOKEN = '?';
    /**
     * The wildcard for multiple characters (*)
     */
    public static final char MULTIPLE_TOKEN = '*';

    /**
     * Performs a match operation using wildcards.
     * 
     * @param patternIndex the patternIndex
     * @param tmpIndex the current index
     * @param value the value
     * @param valueIndex the current value
     * @return true on match
     */
    //CHECKSTYLE:OFF split not possible
    private static boolean wildcardMatch(final String pattern, final int patternIndex, final String value,
            final int valueIndex) {
        int tmpPatternIndex = patternIndex;
        int tmpValueIndex = valueIndex;

        while (tmpPatternIndex < pattern.length()) {
            if (SINGLE_TOKEN == pattern.charAt(tmpPatternIndex)) {
                tmpPatternIndex += 1;

                if (tmpValueIndex < value.length()) {
                    tmpValueIndex += 1;
                } else {
                    return false;
                }
            } else if (MULTIPLE_TOKEN == pattern.charAt(tmpPatternIndex)) {
                while ((tmpPatternIndex < pattern.length())
                        && (MULTIPLE_TOKEN == pattern.charAt(tmpPatternIndex))) {
                    tmpPatternIndex += 1;
                }

                if (tmpPatternIndex >= pattern.length()) {
                    return true;
                }

                while (tmpValueIndex < value.length()) {
                    if (wildcardMatch(pattern, tmpPatternIndex, value, tmpValueIndex)) {
                        return true;
                    }

                    tmpValueIndex += 1;
                }
            } else if ((tmpValueIndex < value.length())
                    && (pattern.charAt(tmpPatternIndex) == value.charAt(tmpValueIndex))) {
                tmpPatternIndex += 1;
                tmpValueIndex += 1;
            } else {
                return false;
            }
        }

        return ((tmpPatternIndex >= pattern.length()) && (tmpValueIndex >= value.length()));
    }
}

Related

  1. wildCardMatch(final String needle, final String haystack)
  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)