Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * 08/06/2004
 *
 * RSyntaxUtilities.java - Utility methods used by RSyntaxTextArea and its
 * views.
 * 
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Main {
    /**
     * Creates a regular expression pattern that matches a "wildcard" pattern.
     * 
     * @param wildcard The wildcard pattern.
     * @param matchCase Whether the pattern should be case sensitive.
     * @param escapeStartChar Whether to escape a starting <code>'^'</code>
     *        character.
     * @return The pattern.
     */
    public static Pattern wildcardToPattern(String wildcard, boolean matchCase, boolean escapeStartChar) {

        int flags = 0;
        if (!matchCase) {
            flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < wildcard.length(); i++) {
            char ch = wildcard.charAt(i);
            switch (ch) {
            case '*':
                sb.append(".*");
                break;
            case '?':
                sb.append('.');
                break;
            case '^':
                if (i > 0 || escapeStartChar) {
                    sb.append('\\');
                }
                sb.append('^');
                break;
            case '\\':
            case '.':
            case '|':
            case '+':
            case '-':
            case '$':
            case '[':
            case ']':
            case '{':
            case '}':
            case '(':
            case ')':
                sb.append('\\').append(ch);
                break;
            default:
                sb.append(ch);
                break;
            }
        }

        Pattern p = null;
        try {
            p = Pattern.compile(sb.toString(), flags);
        } catch (PatternSyntaxException pse) {
            pse.printStackTrace();
            p = Pattern.compile(".+");
        }

        return p;

    }
}