学习后对正则表达式使用记录(个人)

前端之家收集整理的这篇文章主要介绍了学习后对正则表达式使用记录(个人)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package com.LZX.test;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class Tst {
  7.  
  8. public static void main(String[] args) {
  9. /**
  10. * 反斜杠\为java转义符,故需要\\代替,()为正则使用的符号,故也需要转义,使用\\( 正则中: \d 表达为数字,{3}
  11. * 表示匹配3次, \s 表示空格, | 或 , ? 表示个数 0或者1 个,*/
  12. Pattern pattern = Pattern
  13. .compile("((\\(\\d{3}\\)|\\d{3})\\s?\\d{3}-?\\d{4})");
  14.  
  15. String texString = "020237-1212 (020) 237-1212";// (020) 237-1212、020
  16. // 237-1212、020237-1212、0202371212;
  17. Matcher matcher = pattern.matcher(texString);
  18. if (matcher.find()) {
  19. System.out.println(matcher.groupCount());
  20. for (int i = 0; i < matcher.groupCount(); i++) {
  21. System.out.println(matcher.group(i));
  22. }
  23. } else {
  24. System.out.println("not find");
  25. }
  26. }
  27. }
  28. int i = 0;
  29. while(matcher.find()){
  30. i++;
  31. }
  32. System.out.println("find"+i); //通过循环可以统计出现匹配成功次数 每find()一次,则会自减 1

猜你在找的正则表达式相关文章