正则表达式(操作字符串)

正则表达式主要是操作字符串的规则,正则表达式对字符串的操作主要有以下几种应用:
     匹配 matches:
    
    切割 split:
    
     替换replaceAll(String regex, String replacement):
    
    查找:
        指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象,
        依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,
        所以多个匹配器可以共享同一模式。  因此,典型的调用顺序是 
             Pattern p = Pattern.compile("正则");
             Matcher m = p.matcher("aaaaab");
             boolean b = m.matches();
         查找需要使用的对象:
            1.Pattern(正则对象)
            2.Matcher(匹配器对象)
         匹配器要使用的方法:
            1.find()   通知匹配器去匹配字符串,查找符合规则的字符串的子串。如果能查找到符合规则的字符串,则返回true,否则返回false。
            2.group()  获取符合规则的子串 
                 注意:使用group方法的时候一定要先调用find方法让匹配器去查找符合规则的字符串,否则报错。
  
  
  1. package com.cn.regex;
  2. import java.util.Arrays;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. /**
  6. * Author:Liu Zhiyong(QQ:1012421396)
  7. * Version:Version_1
  8. * Date:2016年7月20日10:02:06
  9. * Desc:
  10. 正则表达式主要是操作字符串的规则,正则表达式对字符串的操作主要有以下几种应用:
  11. 匹配 matches:
  12. 切割 split:
  13. 替换replaceAll(String regex, String replacement):
  14. 查找:
  15. 指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象,
  16. 依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,
  17. 所以多个匹配器可以共享同一模式。 因此,典型的调用顺序是
  18. Pattern p = Pattern.compile("正则");
  19. Matcher m = p.matcher("aaaaab");
  20. boolean b = m.matches();
  21. 查找需要使用的对象:
  22. 1.Pattern(正则对象)
  23. 2.Matcher(匹配器对象)
  24. 匹配器要使用的方法:
  25. 1.find() 通知匹配器去匹配字符串,查找符合规则的字符串的子串。如果能查找到符合规则的字符串,则返回true,否则返回false。
  26. 2.group() 获取符合规则的子串
  27. 注意:使用group方法的时候一定要先调用find方法让匹配器去查找符合规则的字符串,否则报错。
  28. */
  29. public class Demo3 {
  30. public static void main(String[] args) {
  31. String phone = "18071897425";
  32. matchesPhone(phone);
  33. phone = "0713-29962208";
  34. matchesTel(phone);
  35. String str = "请 叫 我 木 丁 西";
  36. split1(str);
  37. str = "请叫叫叫叫叫叫叫我木丁丁丁丁丁丁丁丁丁西"; //请 我木 西
  38. split2(str);
  39. str = "如有需求请联系我:18071897425如有需求请联系我:18071897425如有需求请联系我:18071897425如有需求请联系我:18071897425"
  40. + " 如有需求请联系我:18071897425如有需求请联系我:18071897425如有需求请联系我:18071897425如有需求请联系我:18071897425";
  41. String replacement = "***";
  42. replaceAll1(str, replacement);
  43. str = "我我我要要要做做项项项项项项项项项项项项项目目";//还原成:我要做项目 把重叠词替换成单个词
  44. replacement = "\\1";
  45. replaceAll2(str, replacement);
  46. pattern();
  47. }
  48. //正则对象
  49. public static void pattern() {
  50. String str;
  51. //正则对象。。。。。查找
  52. //找出3个字母组成的单词
  53. str = "Qing jiao wo mu ding xi, hen gao xing ren shi ni men";
  54. //先要把字符串的正则编译成正则对象
  55. Pattern p = Pattern.compile("\\b[a-zA-Z]{3}\\b");//加上了边界匹配器
  56. //使用正则对象匹配字符串产生一个匹配器对象
  57. Matcher m = p.matcher(str);
  58. System.out.println("有符合的字符串吗?" + m.find());;
  59. while(m.find()){
  60. System.out.print(m.group() + "\t");
  61. }
  62. }
  63. /**
  64. *需求1:编写一个正则表达式匹配手机号。
  65. *1.第一位:1
  66. *2.第二位:3 4 5 7 8
  67. *3.长度:11位
  68. * @param phone
  69. */
  70. public static void matchesPhone(String phone) {
  71. String regex = "1[34578]\\d{9}";
  72. System.out.println(phone.matches(regex)?"号码格式正常":"非法手机号");
  73. }
  74. /**
  75. * 需求2:匹配固定电话
  76. * 区号-主机号
  77. * 1.区号:首位为0 。。长度3~4位
  78. * 2.主机号:首位不能为0。。长度7~8位
  79. * @param tel
  80. */
  81. public static void matchesTel(String tel){
  82. String regex = "0\\d{2,3}-[1-9]\\d{6,7}";
  83. System.out.println(tel.matches(regex)?"合法固定电话":"非法固定电话");
  84. }
  85. /**
  86. * 按照空格切割
  87. * @param str
  88. */
  89. public static void split1(String str){
  90. String regex = " +";
  91. String[] strs = str.split(regex);
  92. System.out.println(Arrays.toString(strs));
  93. }
  94. /**
  95. * 根据重叠词进行切割
  96. * @param str
  97. */
  98. public static void split2(String str){
  99. String regex = "(.)\\1+";//如果正则的内容需要被复用,那么需要对正则的内容进行分组。分组的目的就是为了提高正则的复用性。组号不能指定,组号从1开始。
  100. String[] strs = str.split(regex);
  101. System.out.println(Arrays.toString(strs));
  102. }
  103. /**
  104. * 替换
  105. * @param str
  106. */
  107. public static void replaceAll1(String str, String replacement){
  108. String regex = "1[34578]\\d{9}";
  109. str = str.replaceAll(regex, replacement);//返回值就是替换后的结果
  110. System.out.println(str);
  111. }
  112. /**
  113. * 替换
  114. * 如果需要在replaceAll方法正则的外部引用组的内容,那么是使用"$组号"
  115. * @param str
  116. * @param replacement
  117. */
  118. public static void replaceAll2(String str, String replacement){
  119. String regex = "(.)\\1+";
  120. // str = str.replaceAll(regex, replacement); //如果需要在replaceAll方法正则的外部引用组的内容,那么是使用"$组号"
  121. str = str.replaceAll(regex, "$1"); //如果需要在replaceAll方法正则的外部引用组的内容,那么是使用"$组号"
  122. System.out.println(str);
  123. }
  124. }

        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值