com.feilong.core.util.RegexUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.util.RegexUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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.feilong.core.util;

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

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.RegexPattern;
import com.feilong.tools.jsonlib.JsonUtil;

import static com.feilong.core.util.MapUtil.newLinkedHashMap;

/**
 * ?.
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see RegexPattern
 * @see java.util.regex.Pattern
 * @see java.util.regex.Matcher
 * @see java.lang.String#matches(String)
 * @since 1.0.0
 * @since jdk1.4
 */
public final class RegexUtil {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(RegexUtil.class);

    /** Don't let anyone instantiate this class. */
    private RegexUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * ? <code>regexPattern</code> ? <code>input</code> ?.
     * 
     * <p>
     * {@link Pattern#matches(String, CharSequence)} {@link #getMatcher(String, CharSequence)}.matches();
     * </p>
     *
     * @param regexPattern
     *            ?,pls use {@link RegexPattern}
     * @param input
     *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
     * @return input ? regex??,true, ? false;<br>
     *          <code>regexPattern</code> null, {@link NullPointerException}<br>
     *          <code>input</code> null, {@link NullPointerException}<br>
     * @see #getMatcher(String, CharSequence)
     * @see Matcher#matches()
     * @see Pattern#matches(String, CharSequence)
     * @since 1.0.7
     */
    public static boolean matches(String regexPattern, CharSequence input) {
        return getMatcher(regexPattern, input).matches();
    }

    /**
     * ??????.
     * 
     * <p>
     * ? m?? s  g,? m.group(g)  s.substring(m.start(g), m.end(g)).<br>
     * ? 1?.0?,? m.group(0) m.group().
     * </p>
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * String regexPattern = "(.*?)@(.*?)";
     * String email = "venusdrogon@163.com";
     * RegexUtil.group(regexPattern, email);
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     *    0 venusdrogon@163.com
     *    1 venusdrogon
     *    2 163.com
     * </pre>
     * 
     * </blockquote>
     * 
     * @param regexPattern
     *            ?,pls use {@link RegexPattern}
     * @param input
     *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
     * @return  <code>regexPattern</code> null, {@link NullPointerException}<br>
     *          <code>input</code> null, {@link NullPointerException}<br>
     *          ??, {@link java.util.Collections#emptyMap()}
     * @see #getMatcher(String, CharSequence)
     * @see Matcher#group(int)
     * @since 1.0.7
     */
    public static Map<Integer, String> group(String regexPattern, CharSequence input) {
        Matcher matcher = getMatcher(regexPattern, input);
        if (!matcher.matches()) {
            LOGGER.trace("[not matches] ,\n\tregexPattern:[{}] \n\tinput:[{}]", regexPattern, input);
            return Collections.emptyMap();
        }
        int groupCount = matcher.groupCount();
        Map<Integer, String> map = newLinkedHashMap(groupCount + 1);
        for (int i = 0; i <= groupCount; ++i) {
            //?
            String groupValue = matcher.group(i); //map.put(0, matcher.group());// ? 1 ?.0?,? m.group(0)  m.group().
            LOGGER.trace("matcher group[{}],start-end:[{}-{}],groupValue:[{}]", i, matcher.start(i), matcher.end(i),
                    groupValue);
            map.put(i, groupValue);//groupValue
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("regexPattern:[{}],input:[{}],groupMap:{}", regexPattern, input, JsonUtil.format(map));
        }
        return map;
    }

    /**
     * ??????.
     * 
     * <p>
     * ? m?? s  g,? m.group(g)  s.substring(m.start(g), m.end(g)).<br>
     * ? 1?.0?,? m.group(0) m.group().
     * </p>
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * 
     * String regexPattern = "(.*?)@(.*?)";
     * String email = "venusdrogon@163.com";
     * LOGGER.info(RegexUtil.group(regexPattern, email, 1) + "");//venusdrogon
     * LOGGER.info(RegexUtil.group(regexPattern, email, 2) + "");//163.com
     * 
     * </pre>
     * 
     * </blockquote>
     *
     * @param regexPattern
     *            ?,pls use {@link RegexPattern}
     * @param input
     *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
     * @param group
     *            the group
     * @return  <code>regexPattern</code> null, {@link NullPointerException}<br>
     *          <code>input</code> null, {@link NullPointerException}<br>
     * @see #getMatcher(String, CharSequence)
     * @see Matcher#group(int)
     * @since 1.0.7
     */
    public static String group(String regexPattern, CharSequence input, int group) {
        Map<Integer, String> map = group(regexPattern, input);
        return map.get(group);
    }

    //********************************************************************************************
    /**
     * Gets the matcher.
     *
     * @param regexPattern
     *            ?,pls use {@link RegexPattern}
     * @param input
     *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
     * @return  <code>regexPattern</code> null, {@link NullPointerException}<br>
     *          <code>input</code> null, {@link NullPointerException}<br>
     * @see Pattern#compile(String)
     * @since 1.0.7
     */
    private static Matcher getMatcher(String regexPattern, CharSequence input) {
        return getMatcher(regexPattern, input, 0);
    }

    /**
     * Gets the matcher.
     *
     * @param regexPattern
     *            ?,pls use {@link RegexPattern}
     * @param input
     *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
     * @param flags
     *            ??,?
     *            <blockquote><code>Pattern.compile(regex, CASE_INSENSITIVE | DOTALL);</code></blockquote>
     *            <ul>
     *            <li>{@link Pattern#CASE_INSENSITIVE} ??,?US ASCII.</li>
     *            <li>{@link Pattern#MULTILINE} ^$?,?</li>
     *            <li>{@link Pattern#UNICODE_CASE} CASE_INSENSITIVE?,Unicode??</li>
     *            <li>{@link Pattern#CANON_EQ} Unicode</li>
     *            <li>{@link Pattern#DOTALL} ,.??</li>
     *            <li>{@link Pattern#UNIX_LINES} ??^$,?'\n'</li>
     *            <li>{@link Pattern#LITERAL} ????.</li>
     *            <li>{@link Pattern#COMMENTS} ??. <br>
     *            ??? # ?. <br>
     *            ?? (?x) ???. <br>
     *            </li>
     *            </ul>
     * @return  <code>regexPattern</code> null, {@link NullPointerException}<br>
     *          <code>input</code> null, {@link NullPointerException}<br>
     * @see Pattern#compile(String, int)
     * @since 1.5.3
     */
    private static Matcher getMatcher(String regexPattern, CharSequence input, int flags) {
        Validate.notNull(regexPattern, "regexPattern can't be null!");
        Validate.notNull(input, "input can't be null!");
        Pattern pattern = Pattern.compile(regexPattern, flags);
        return pattern.matcher(input);
    }
}