首页 > 其他 > 详细

正则表达式m.find() and m.group()

时间:2021-09-12 00:23:04      阅读:48      评论:0      收藏:0      [点我收藏+]

问题:

在用正则表达式截取字符串时,要先执行m.find(),再输出m.group()。否则会返回"No match found" 异常,为什么?

 

代码:

public static String truncateSentence(String s,int k) {
		String regex = "(\\w+\\s{1})"+"{"+ k + "}";
		Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        //throw "No match found" exception if m.find() is not executed.
        //m.find();
        return m.group();
	}

  

正则表达式m.find() and m.group()

原文:https://www.cnblogs.com/cheese320/p/15253524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!