Converts a Unix-style glob to a regular expression. - Java Language Basics

Java examples for Language Basics:Console

Description

Converts a Unix-style glob to a regular expression.

Demo Code

/*//  w  w w  . j  av  a2 s.c  o m
 * MiscUtilities.java - Various miscallaneous utility functions
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2004 Slava Pestov
 * Portions copyright (C) 2000 Richard S. Hall
 * Portions copyright (C) 2001 Dirk Moebius
 *
 * 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 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.
 */
//package com.java2s;
import java.util.Stack;

public class Main {
    /**
     * Converts a Unix-style glob to a regular expression.
     * <p>
     *  ? becomes ., * becomes .*, {aa,bb} becomes (aa|bb).
     * @param glob The glob pattern
     */
    public static String globToRE(String glob) {
        final Object NEG = new Object();
        final Object GROUP = new Object();
        Stack state = new Stack();
        StringBuffer buf = new StringBuffer();
        boolean backslash = false;
        for (int i = 0; i < glob.length(); i++) {
            char c = glob.charAt(i);
            if (backslash) {
                buf.append('\\');
                buf.append(c);
                backslash = false;
                continue;
            }
            switch (c) {
            case '\\':
                backslash = true;
                break;
            case '?':
                buf.append('.');
                break;
            case '.':
            case '+':
            case '(':
            case ')':
                buf.append('\\');
                buf.append(c);
                break;
            case '*':
                buf.append(".*");
                break;
            case '|':
                if (backslash)
                    buf.append("\\|");
                else
                    buf.append('|');
                break;
            case '{':
                buf.append('(');
                if (i + 1 != glob.length() && glob.charAt(i + 1) == '!') {
                    buf.append('?');
                    state.push(NEG);
                } else
                    state.push(GROUP);
                break;
            case ',':
                if (!state.isEmpty() && state.peek() == GROUP)
                    buf.append('|');
                else
                    buf.append(',');
                break;
            case '}':
                if (!state.isEmpty()) {
                    buf.append(")");
                    if (state.pop() == NEG)
                        buf.append(".*");
                } else
                    buf.append('}');
                break;
            default:
                buf.append(c);
            }
        }
        return buf.toString();
    }
}

Related Tutorials