zoukankan      html  css  js  c++  java
  • BigData09 正则表达式及应用 compareTo源码分析和应用

    1.正则表达式

     1 /*
     2  * 邮箱验证
     3  * x_xx@scs.com.cn
     4  * abc@df.com
     5  * a|b a或者b
     6  * X?  X一次或一次也没有 
     7  * X*  X零次或多次 
     8  * X+  X一次或多次 
     9  * X{n,}  X至少 n 次 
    10  * ^ 行的开头  $ 行的结尾 
    11  */
    12 import java.util.regex.Matcher;
    13 import java.util.regex.Pattern;
    14 
    15 public class Demo{
    16     public static void main(String args[]) {
    17         String str = "sian_aax@qq.com.cn";
    18         String pattern = "^([a-z0-9A-Z]+[_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(_[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    19 
    20         Pattern r = Pattern.compile(pattern);
    21         Matcher m = r.matcher(str);
    22         System.out.println(m.matches());
    23     }
    24 }
    25     
    正则表达式_邮箱验证
     1 /*
     2  * \b 单词边界 
     3  * \w 单词字符:[a-zA-Z_0-9] 
     4  * X{n} X,恰好 n 次 
     5  */
     6 import java.util.regex.Matcher;
     7 import java.util.regex.Pattern;
     8 
     9 public class PatternDemo2 {
    10     public static void main(String[] args) {
    11         String s = "The Java language provides special support for the string "+
    12   "concatenation operator ( + ), and for conversion of"+
    13   "other objects to strings. String concatenation is implemented"+
    14   "through the {@code StringBuilder}(or {@code StringBuffer})";
    15  
    16         //获取所有两个字符的单词
    17         Pattern p = Pattern.compile("\\b\\w{3}\\b");
    18         //返回一个字符串的匹配器对象
    19         Matcher m = p.matcher(s);
    20         
    21 /*        boolean b1 = m.find();
    22         if(b1){
    23             String str = m.group();
    24             System.out.println(str);
    25         }
    26         
    27         b1 = m.find();
    28         if(b1){
    29             String str = m.group();
    30             System.out.println(str);
    31         }
    32 
    33         b1 = m.find();
    34         if(b1){
    35             String str = m.group();
    36             System.out.println(str);
    37         }*/
    38         
    39         while(m.find()){
    40             System.out.println(m.group());
    41         }    
    42     }
    43 }
    利用正则表达式找出一段文字的3个字母单词
     1 /*
     2  * 切割功能使用正则表达式:
     3  */
     4 public class RegexDemo4 {
     5 
     6     public static void main(String[] args) {
     7 //        String s = "hello world  hi   hehe    mysql        oracle";
     8         //拆分字符串  一个以上空格都拆
     9 //        String[] arr = s.split(" +");
    10         
    11         
    12 //        String s = "hello.world.hi.hehe.mysql.oracle";
    13 //        String[] arr = s.split("\\.");
    14         
    15         String s = "c:\\code\\day11\\test";
    16         //拆分字符串\\\\把\\拆出
    17         String[] arr = s.split("\\\\");
    18         
    19         
    20         for (int i = 0; i < arr.length; i++) {
    21             System.out.println(arr[i]);
    22         }
    23     }
    24 }
    切割功能使用正则表达式
     1 /*
     2  * 正则表达式实现替换 ***
     3  */
     4 public class RegexDemo5 {
     5 
     6     public static void main(String[] args) {
     7 //        String s = "Hello world Hello world";
     8 //        String replace = s.replace('l', 'L');
     9 //        System.out.println(replace);
    10         
    11 //        String replace = s.replace("world", "China");
    12 //        System.out.println(replace);
    13 //
    14 //        String replace = s.replace("world", "China");
    15 //        System.out.println(replace);
    16         
    17         //第一次索引遇到的l替换成A
    18 //        String replaceFirst = s.replaceFirst("l", "A");
    19 //        System.out.println(replaceFirst);
    20         
    21         //字符串中索引的l替换成A
    22 //        String replaceAll = s.replaceAll("l", "A");
    23 //        System.out.println(replaceAll);
    24         
    25         //要求:替换一个字符串中所有的"账号" --> "**"
    26         
    27         String s = "zhangsan账号123456789";
    28 //        String replace = s.replace("账号", "**");
    29 //        System.out.println(replace);
    30         //要求:替换一个字符串中所有的数字到*
    31 //        String replaceAll = s.replaceAll("[0-9]+", "*");
    32 //        System.out.println(replaceAll);
    33         //遇到数字就替换成*
    34         String replaceAll = s.replaceAll("[0-9]", "*");
    35         System.out.println(replaceAll);    
    36     }
    37 }
    正则表达式实现替换 ***

    2.compareTo源码分析

     1 public class CompareDemo {
     2 
     3     public static void main(String[] args) {
     4         /*String s1 = "Hello";
     5         String s2 = "Hello";
     6         System.out.println(s1 == s2);
     7         System.out.println(s1.equals(s2));
     8         System.out.println(s1.compareTo(s2));//0
     9         */
    10         
    11         String ss1 = "ABCE";
    12         String ss2 = "ABCD";
    13         System.out.println(ss2.compareTo(ss1));//-1
    14         
    15         System.out.println("----------------");
    16         String s1 = "ABCDEFG";
    17         String s2 = "ABCDE";
    18         System.out.println(s1.compareTo(s2));//2
    19         
    20         
    21 //        String res = s1.replace('l', 'L');
    22 //        System.out.println(res);
    23     }
    24 
    25 }
    compareTo应用
  • 相关阅读:
    SDN第三次上机作业
    团队作业——Beta冲刺
    SDN第二次上机作业
    在mpvue中使用map如何避坑
    仿一个好玩的滑动效果
    乡音
    mpvue支持小程序的分包加载
    台风🌀和口腔溃疡
    记一次cocos项目的加载速度优化
    如何用ajax下载文件
  • 原文地址:https://www.cnblogs.com/star521/p/8401550.html
Copyright © 2011-2022 走看看