Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (c) 2013, 2014 Denis Nikiforov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Denis Nikiforov - initial API and implementation
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String matchCamelCase(String query, String str) {
        if (!query.matches("[A-Za-z\\*]+")) {
            return null;
        }
        String head = "";
        int i;
        for (i = 0; i < query.length(); i++) {
            char charI = query.charAt(i);
            if (Character.isLowerCase(charI)) {
                head += charI;
            } else {
                break;
            }
        }
        if (i > 0) {
            head += "[^A-Z]*";
        }
        String tail = query.substring(i);
        String re = "\\b(";
        tail = tail.replaceAll("\\*", ".*?");
        re += head + tail.replaceAll("([A-Z][^A-Z]*)", "$1[^A-Z]*");
        re += ".*?)\\b";
        Pattern regex = Pattern.compile(re);
        Matcher m = regex.matcher(str);
        if (m.find()) {
            return m.group();
        } else {
            return null;
        }
    }
}