Java 正则表达式重复匹配篇

编程入门 行业动态 更新时间:2024-10-21 06:29:28

Java <a href=https://www.elefans.com/category/jswz/34/1770561.html style=正则表达式重复匹配篇"/>

Java 正则表达式重复匹配篇

重复匹配

  • * 可以匹配任意个字符,包括0个字符。
  • + 可以匹配至少一个字符。
  • ? 可以匹配0个或一个字符。
  • {n} 可以精确指定 n 个字符。
  • {n,m} 可以精确匹配 n-m 个字符。你可以是 0 。

匹配任意个字符

匹配 D 开头,后面是任意数字的字符,

        String regexU1 = "D\\d*";System.out.println("DD".matches(regexU1));// falseSystem.out.println("D1".matches(regexU1));// trueSystem.out.println("D202".matches(regexU1));// trueSystem.out.println("D88888888".matches(regexU1));// true

匹配至少一个字符

匹配 D 开头,后面至少是一位数字的字符,

        String regexU2 = "D\\d+";System.out.println("D".matches(regexU2));// falseSystem.out.println("D1".matches(regexU2));// trueSystem.out.println("D202".matches(regexU2));// trueSystem.out.println("D88888888".matches(regexU2));// true

匹配 0 个或一个字符

匹配 D 开头,后面是 0个或一个数字的字符,

        String regexU3 = "D\\d?";System.out.println("D".matches(regexU3));// trueSystem.out.println("D1".matches(regexU3));// trueSystem.out.println("D22".matches(regexU3));// falseSystem.out.println("D88888888".matches(regexU3));// false

匹配 n 个字符

匹配 D 开头,后面 3 个数字的字符,

        String regexU4 = "D\\d{3}";System.out.println("D".matches(regexU4));// falseSystem.out.println("D1".matches(regexU4));// falseSystem.out.println("D22".matches(regexU4));// falseSystem.out.println("D301".matches(regexU4));// trueSystem.out.println("D3004".matches(regexU4));// false

匹配 n-m 个字符

匹配 D 开头,后面是 3-5 位数字的字符,

        String regexU5 = "D\\d{3,5}";System.out.println("D".matches(regexU5));// falseSystem.out.println("D1".matches(regexU5));// falseSystem.out.println("D22".matches(regexU5));// falseSystem.out.println("D333".matches(regexU5));// trueSystem.out.println("D4000".matches(regexU5));// trueSystem.out.println("D55555".matches(regexU5));// trueSystem.out.println("D666666".matches(regexU5));// false

更多推荐

Java 正则表达式重复匹配篇

本文发布于:2023-11-16 17:53:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!