在Java中用str.matches(reg)的方式是返回的一个布尔值,而我们有的时候需要返回匹配到的值的内容,具体实现代码如下:

public static void main(String[] args) {
        String reg = "2[0-9]{11}-{0,1}[0-9]{0,1}";
        Pattern p = Pattern.compile(reg);
        String str = "200000000000-2200000000000";
        Matcher m =p.matcher(str);
        while(m.find())
            System.out.println(m.group(0));
            
    }

这样就把匹配到的所有内容输出来了。

相关文章:

  • 2022-02-16
  • 2022-12-23
  • 2021-11-17
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-10-12
猜你喜欢
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2021-11-04
相关资源
相似解决方案