Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.regex.*;

public class Main {
    /**
     * Compiles the given regular expression into a Pattern that may or may not be case-sensitive, depending on the regular expression.
     * If the regular expression contains any capital letter, that is assumed to be meaningful, and the resulting Pattern is case-sensitive.
     * If the whole regular expression is lower-case, the resulting Pattern is case-insensitive.
     * 
     * This is useful for implementing functionality like emacs/vim's "smart case", hence the name.
     * 
     * By default, we enable (?m) on the assumption that it's more useful if ^ and $ match the start and end of each line rather than just the start and end of input.
     */
    public static Pattern smartCaseCompile(String regularExpression) {
        boolean caseInsensitive = true;
        for (int i = 0; i < regularExpression.length(); ++i) {
            if (Character.isUpperCase(regularExpression.charAt(i))) {
                caseInsensitive = false;
            }
        }
        int flags = Pattern.MULTILINE;
        if (caseInsensitive) {
            flags |= Pattern.CASE_INSENSITIVE;
        }
        return Pattern.compile(regularExpression, flags);
    }
}