用例子来学习正则表达式

前端之家收集整理的这篇文章主要介绍了用例子来学习正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
 
  1. importjava.util.regex.Matcher;
  2. importjava.util.regex.Pattern;
  3. /**
  4. *正则表达式:字符串的处理利器
  5. *@authorkenan
  6. *
  7. */
  8. publicclassRegularExpressions{
  9. publicstaticvoidmain(String[]args){
  10. //在正则表达式中.代表一个字符
  11. //System.out.println("abc".matches("..."));
  12. //\d代表一个数字:使用表达式进行匹配替换
  13. //System.out.println("ab12356c".replaceAll("\\d","-"));
  14. //这个是我们平时的用法
  15. //System.out.println("ab12346c".replaceAll("123","---"));
  16. //编译一个正则表达式
  17. //Patternp=Pattern.compile("[a-z]{3}");
  18. //匹配一个字符串,返回一个Matcher对象
  19. //Matcherm=p.matcher("abc");
  20. //System.out.println(m.matches());
  21. //.:一个字符
  22. //*:0或多个字符
  23. //+:1或者多个
  24. //?:0或者1个
  25. //System.out.println("a".matches("."));
  26. //System.out.println("aa".matches("aa"));
  27. //System.out.println("aaaa".matches("a*"));
  28. //System.out.println("".matches("a*"));
  29. //System.out.println("aaa".matches("a?"));
  30. //System.out.println("".matches("a?"));
  31. //System.out.println("a".matches("a?"));
  32. //System.out.println("123456".matches("\\d{3,100}"));
  33. //System.out.println("12".matches("\\d{3,100}"));
  34. //System.out.println("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}"));
  35. //System.out.println("192.168.0.2".matches("\\d{1,3}"));
  36. //System.out.println("192".matches("[0-2][0-9][0-9]"));
  37. //System.out.println("192".matches("[0-9]"));
  38. //范围匹配
  39. //System.out.println("a".matches("[abc]"));
  40. //System.out.println("a".matches("[^abc]"));
  41. //System.out.println("A".matches("[a-zA-Z1-9]"));
  42. //System.out.println("3".matches("[a-zA-Z1-9]"));
  43. //System.out.println("A".matches("[a-z]|[A-Z]"));
  44. //System.out.println("R".matches("[A-Z&&[RFG]]"));
  45. //认识\s\w\d\
  46. //\d一个数字[0-9]
  47. //\D上面取反
  48. //\s一个空字符[\t\n\r\f\x0B]
  49. //\S上面取反
  50. //\w一个字符[a-zA-Z_0-9]
  51. //\W出了上面的字符以为的其他字符
  52. //\\代表一个\
  53. //System.out.println("\n\r\t".matches("\\s{1,4}"));
  54. //System.out.println("".matches("\\S"));
  55. //System.out.println("a_8".matches("\\w{1,3}"));
  56. //System.out.println("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
  57. //System.out.println("\\".matches("\\\\"));
  58. //PosixStyle
  59. //System.out.println("a".matches("\\p{Lower}"));
  60. //边界处理
  61. //^在[]里代表取反在外面代表字符开头
  62. //$代表以什么结尾
  63. //\b代表单词结束
  64. //System.out.println("hellosir".matches("^h.*"));
  65. //System.out.println("hellosir".matches(".*ir$"));
  66. //System.out.println("hellosirr".matches(".*ir$"));
  67. //System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
  68. //System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
  69. //System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
  70. //System.out.println("hello&sir".matches("^h[a-z]{1,3}o\\b.*"));
  71. //System.out.println("hello.sir".matches("^h[a-z]{1,3}o\\b.*"));
  72. //找出空白行
  73. //System.out.println("\n".matches("^[\\s&&[^\\n]]*\\n$"));
  74. //System.out.println("\n".matches("^[\\s&&[^\\n]]*\\n$"));
  75. //email
  76. //System.out.println("soukenan@qq.com".matches("[\\w.-]+@[\\w.-]+\\.[\\w]+"));
  77. //matchesfromlookingat
  78. //Patternp=Pattern.compile("\\d{3,5}");
  79. //Strings="123-45674-234-00";
  80. //Matcherm=p.matcher(s);
  81. //System.out.println(m.matches());//false//从前往后匹配
  82. //m.reset();//把光标重置
  83. //System.out.println(m.find());//true//从当前的标志开始查找
  84. //System.out.println(m.start()+":"+m.end());
  85. //System.out.println(m.find());//true查找匹配的字串
  86. //System.out.println(m.start()+":"+m.end());
  87. //System.out.println(m.find());//true
  88. //System.out.println(m.start()+":"+m.end());
  89. //System.out.println(m.find());//false
  90. //
  91. //System.out.println(m.lookingAt());//true//从头开始看看是否匹配
  92. //System.out.println(m.lookingAt());//true
  93. //System.out.println(m.lookingAt());//true
  94. //System.out.println(m.lookingAt());//true
  95. //replacement字符串的替换
  96. //Patternp=null;
  97. //p=Pattern.compile("java");
  98. //p=Pattern.compile("java",Pattern.CASE_INSENSITIVE);//忽略大小写
  99. //Matcherm=p.matcher("javaJavaJaVajavA,iloveJAVA!");
  100. //while(m.find()){
  101. //System.out.println(m.group());
  102. //}
  103. //System.out.println(m.replaceAll("JAVA"));//把找到的匹配字符串转换成JAVA
  104. //StringBufferbuf=newStringBuffer();
  105. //inti=0;
  106. //while(m.find()){
  107. //i++;
  108. //if(i%2==0){
  109. //m.appendReplacement(buf,"java");//偶数转换成java
  110. //}else{
  111. //m.appendReplacement(buf,"JAVA");//奇数转换成JAVA
  112. //}
  113. //}
  114. //m.appendTail(buf);//把尾部字符串添加上去
  115. //System.out.println(buf);
  116. //group分组
  117. //()这个是用来分组的
  118. Patternp=Pattern.compile("(\\d{3,5})([a-z]{2})");
  119. Strings="123aa-3456bb-234cc-00";
  120. Matcherm=p.matcher(s);
  121. while(m.find()){
  122. System.out.println(m.group(1)+":"+m.group(2));
  123. }
  124. }
  125. }

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