java正则总是:java.lang.IllegalStateException: No match found 的分析解决

来源:互联网 发布:大麦户2016源码 编辑:程序博客网 时间:2024/09/21 09:03
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 

阅读全文
0 0