public static Boolean matches( String text , String pattern , String escapeChar )
    {
        if (pattern.length() == 0)
            return text.length() == 0 ? Boolean.TRUE : Boolean.FALSE;
        
        int textPos = 0;
        boolean startWithAny = false;
        String separators = "%_";
        if (escapeChar != null)
            separators += escapeChar;
        StringTokenizer st = new StringTokenizer(pattern,separators,true);
        boolean escapeNext = false;
        while (st.hasMoreTokens())
        {
            String subPattern = st.nextToken();
            
            if (escapeChar != null && !escapeNext && subPattern.equals(escapeChar))
            {
                escapeNext = true;
            }
            else
            if (!escapeNext && subPattern.equals("%"))
            {
                startWithAny = true;
            }
            else
            if (!escapeNext && subPattern.equals("_"))
            {
                if (textPos < text.length())
                    textPos++;
                else
                    return Boolean.FALSE;
            }
            else
            {
                if (startWithAny)
                {
                    int matchPos = text.lastIndexOf(subPattern);
                    if (matchPos == -1 || matchPos < textPos) 
                        return Boolean.FALSE;
                    textPos = matchPos + subPattern.length();
                    startWithAny = false;
                }
                else
                {
                    if (!text.startsWith(subPattern,textPos))
                        return Boolean.FALSE;
                    else
                        textPos += subPattern.length();
                }
                escapeNext = false;
            }
        }
        
        return Boolean.TRUE;
    }
public static Boolean matches ( String text , String pattern , String escapeChar ) { if ( pattern . length ( ) == 0 ) return text . length ( ) == 0 ? Boolean . TRUE : Boolean . FALSE ; int textPos = 0 ; boolean startWithAny = false ; String separators = "%_" ; if ( escapeChar != null ) separators += escapeChar ; StringTokenizer st = new StringTokenizer ( pattern , separators , true ) ; boolean escapeNext = false ; while ( st . hasMoreTokens ( ) ) { String subPattern = st . nextToken ( ) ; if ( escapeChar != null && ! escapeNext && subPattern . equals ( escapeChar ) ) { escapeNext = true ; } else if ( ! escapeNext && subPattern . equals ( "%" ) ) { startWithAny = true ; } else if ( ! escapeNext && subPattern . equals ( "_" ) ) { if ( textPos < text . length ( ) ) textPos ++ ; else return Boolean . FALSE ; } else { if ( startWithAny ) { int matchPos = text . lastIndexOf ( subPattern ) ; if ( matchPos == - 1 || matchPos < textPos ) return Boolean . FALSE ; textPos = matchPos + subPattern . length ( ) ; startWithAny = false ; } else { if ( ! text . startsWith ( subPattern , textPos ) ) return Boolean . FALSE ; else textPos += subPattern . length ( ) ; } escapeNext = false ; } } return Boolean . TRUE ; }
Check if the given text matches a pattern