java.lang.IllegalStateException: No match found

package com.quanran;

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

public class RegexTest {

	/**
	 * <p>Discription:[根据传入的regex正则表达式,找到source中的匹配部分]</p>
	 * Created on 2017年12月22日 下午2:12:12
	 * @param regex 正则表达式
	 * @param source 源文件
	 * @return String 返回匹配到的字符串
	 * @author:[全冉]
	 */
	private static String getMatcher(String regex, String source) {  
		String result = "";  
		Pattern pattern = Pattern.compile(regex);  
		Matcher matcher = pattern.matcher(source);  
		while (matcher.find()) {  
			result = matcher.group();
		}  
		return result;  
	}  
	
	public static void main(String[] args) {
		String str = getMatcher("\\d{11}$", "全冉15175223269");
		System.out.println(str);
	}
}
备注:想要matcher.group()方法,必须先matcher.find()方法,切忌!要不然总是报:

java.lang.IllegalStateException: No match found 

猜你喜欢

转载自blog.csdn.net/qq_23167527/article/details/78873054
今日推荐