问题:

在用正则表达式截取字符串时,要先执行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();
	}

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2021-11-22
  • 2021-10-29
  • 2021-07-01
  • 2021-08-01
猜你喜欢
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-10-19
相关资源
相似解决方案