转换Unix

欢马劈雪     最近更新时间:2020-01-02 10:19:05

557
将Unix样式的glob转换为正则表达式。

/*

 * 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.nowjava;
/**
来 自 N o w J a v a . c o m
**/

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;

            }
            /*
             from 时代Java - N o w  J a v a . c o m 
            */

            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('|');

          
展开阅读全文