// Path: src/main/java/com/github/crab2died/exceptions/IllegalGroupIndexException.java
// public class IllegalGroupIndexException extends RuntimeException {
// 	
// 	private static final long serialVersionUID = 7725478743860387475L;
// 
// 	public IllegalGroupIndexException(String message) {
//         super(message);
//     }
// }


import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.github.crab2died.exceptions.IllegalGroupIndexException;
import java.util.ArrayList;
import java.util.List;
/*
 *
 *                  Copyright 2017 Crab2Died
 *                     All rights reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 * Browse for more information ：
 * 1) https://gitee.com/Crab2Died/Excel4J
 * 2) https://github.com/Crab2died/Excel4J
 *
 */

package com.github.crab2died.utils;



/**
 * <p>正则匹配相关工具</p>
 * author : Crab2Died
 * date : 2017/5/24  9:43
 */
public class RegularUtils {


    /**
     * <p>判断内容是否匹配</p>
     * author : Crab2Died
     * date   : 2017年06月02日  15:46:25
     *
     * @param pattern 匹配目标内容
     * @param reg     正则表达式
     * @return 返回boolean
     */
    public static boolean isMatched(String pattern, String reg) {
        Pattern compile = Pattern.compile(reg);
        return compile.matcher(pattern).matches();
    }

    /**
     * <p>正则提取匹配到的内容</p>
     * <p>例如：</p>
     * <p>
     * author : Crab2Died
     * date   : 2017年06月02日  15:49:51
     *
     * @param pattern 匹配目标内容
     * @param reg     正则表达式
     * @param group   提取内容索引
     * @return 提取内容集合
     */
    public static List<String> match(String pattern, String reg, int group) {

        List<String> matchGroups = new ArrayList<>();
        Pattern compile = Pattern.compile(reg);
        Matcher matcher = compile.matcher(pattern);
        if (group > matcher.groupCount() || group < 0)
// Path: src/main/java/com/github/crab2died/exceptions/IllegalGroupIndexException.java
// public class IllegalGroupIndexException extends RuntimeException {
// 	
// 	private static final long serialVersionUID = 7725478743860387475L;
// 
// 	public IllegalGroupIndexException(String message) {
//         super(message);
//     }
// }


// Path: src/main/java/com/github/crab2died/utils/RegularUtils.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.github.crab2died.exceptions.IllegalGroupIndexException;
import java.util.ArrayList;
import java.util.List;

/*
 *
 *                  Copyright 2017 Crab2Died
 *                     All rights reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 * Browse for more information ：
 * 1) https://gitee.com/Crab2Died/Excel4J
 * 2) https://github.com/Crab2died/Excel4J
 *
 */

package com.github.crab2died.utils;



/**
 * <p>正则匹配相关工具</p>
 * author : Crab2Died
 * date : 2017/5/24  9:43
 */
public class RegularUtils {


    /**
     * <p>判断内容是否匹配</p>
     * author : Crab2Died
     * date   : 2017年06月02日  15:46:25
     *
     * @param pattern 匹配目标内容
     * @param reg     正则表达式
     * @return 返回boolean
     */
    public static boolean isMatched(String pattern, String reg) {
        Pattern compile = Pattern.compile(reg);
        return compile.matcher(pattern).matches();
    }

    /**
     * <p>正则提取匹配到的内容</p>
     * <p>例如：</p>
     * <p>
     * author : Crab2Died
     * date   : 2017年06月02日  15:49:51
     *
     * @param pattern 匹配目标内容
     * @param reg     正则表达式
     * @param group   提取内容索引
     * @return 提取内容集合
     */
    public static List<String> match(String pattern, String reg, int group) {

        List<String> matchGroups = new ArrayList<>();
        Pattern compile = Pattern.compile(reg);
        Matcher matcher = compile.matcher(pattern);
        if (group > matcher.groupCount() || group < 0)
            throw new IllegalGroupIndexException("Illegal match group :" + group);