Return a regular expression for the given camel-case string. - Java java.lang

Java examples for java.lang:String Camel Case

Description

Return a regular expression for the given camel-case string.

Demo Code

/*/*from  ww w .  j av  a 2  s. c  om*/
 * Copyright (c) 2011, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "java2s.com";
        System.out.println(getCamelCaseRegExp(s));
    }

    /**
     * Return a regular expression for the given camel-case string.
     * 
     * <pre>
     *   AnaSer   => Ana[a-z]*?Ser[a-z]*?
     *   AngCEI   => Ang[a-z]*?C[a-z]*?E[a-z]*?I[a-z]*?
     *   arP      => ar[a-z]*?P[a-z]*?
     * </pre>
     */
    public static String getCamelCaseRegExp(String s) {
        StringBuilder buf = new StringBuilder();
        int index = 0;
        while (index < s.length()) {
            char c = s.charAt(index++);
            buf.append("(");
            buf.append(c);
            while (index < s.length()) {
                char next = s.charAt(index);
                if (Character.isDigit(next) || Character.isLowerCase(next)) {
                    buf.append(next);
                    index++;
                } else {
                    break;
                }
            }
            buf.append(")");
            buf.append("[a-z]*?");
        }
        return buf.toString();
    }
}

Related Tutorials