Pattern Matches file name : Name « Regular Expressions « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Regular Expressions » Name 




Pattern Matches file name
 
/** This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 [email protected]
 */

import java.util.Iterator;
import java.util.List;

public class FileUtils
{
    static public boolean patternMatches(String pattern, String name)
    {
        if (pattern == null)
            return true;
        
        int     iExpr = 0
                iPath = 0,
                lenPattern = pattern.length(),
                lenName = name.length();
        boolean ok = false;

loop:
        while (iExpr < lenPattern)
        {
            char charExpr = pattern.charAt(iExpr);

            switch (charExpr)
            {
                case '?':
                {
                    // any character
                    
                    if (iPath >= lenName)
                        break loop;

                    ++iExpr;
                    ++iPath;
                    break;
                }

                case '*':
                {
                    // character sequence 
                    
                    int nqu = 0;
                    
                    // find normal chars in pattern after '*', count '?'s

                    while (++iExpr < lenPattern)
                    {
                        char c = pattern.charAt(iExpr);

                        if (c == '?')
                            ++nqu;
                        else if (c != '*')
                            break;
                    }
                     
                    if (iExpr >= lenPattern)
                    {
                        // at end, no normal chars after *, just check '?'s
                           
                        ok = lenName - iPath >= nqu;
                        break loop;
                    }
                    else
                    {
                        // extract normal part

                        int jex = iExpr;

                        while (++jex < lenPattern)
                        {
                            char c = pattern.charAt(jex);

                            if (c == '*' || c == '?')
                                break;
                        }
                    
                        String  normal = pattern.substring(iExpr, jex);
                        int     ifound = name.indexOf(normal, iPath);

                        if (ifound < || ifound - iPath < nqu)
                        {
                            // not found in name, or too early
                               
                            break loop;
                        }

                        // adjust indices

                        iPath = ifound + normal.length();
                        iExpr = jex;

                        break;
                    }
                }
                
                default:
                {
                    // normal char
                       
                    if (iPath >= lenName || charExpr != name.charAt(iPath))
                        break loop;

                    ++iExpr;
                    ++iPath;
                }
            }
        }

        if (! ok
            ok = iPath == lenName;

        return ok;
        
    }

    //----------------------------------------------------------------------

    static public boolean patternMatches(List patterns, String name)
    {
        if (patterns == null)
            return false;

        for (Iterator it = patterns.iterator(); it.hasNext())
        {
            String pat = (String)it.next();

            if (patternMatches(pat, name))
                return true;
        }

        return false;
    }
                    
    
}

   
  














Related examples in the same category
1.Match NameMatch Name
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.