美文网首页
Java基础笔记总结(8)-常见类(2)正则表达式(Patter

Java基础笔记总结(8)-常见类(2)正则表达式(Patter

作者: 吵吵先生 | 来源:发表于2019-01-19 22:52 被阅读0次

    一、正则表达式的使用

    用来描述和匹配一系列符合某个语法规则的字符串,有自己的特殊的应用

    例如注册邮箱,用户名和密码的限制

    String regex ="";

    public boolean matches(String regex) 用匹配方法判断正负

    其中 字符类演示 []代表单个字符

    [abc] a、b或c

    [^abc]除了abc以外的任意字符

    [a-zA-Z]a到z 或者A到Z 两头字母包括在内(范围)

    [a-d[m-p]] a到d 或者m到p[a-dm-p] 并集

    [a-z&&[def]]交集 包含共有的字符

    [a-z&&[^bc]]a-z中 出去bc [ad-z]

    [a-z&&[^m-p]]a-z中 但是不在m-p [a-lq-z]

    [0-9]0-9

    预定义字符表达式:注意转义字符所出现的问题

    .任何字符

    \d 数字:[0-9]

    \D 非数字[^0-9]

    \s 空白字符[ \t\n\x0B\f\r]

    \S 非空白字符[^\s]

    \w 单词字符:[a-zA-z_0-9]

    \W 非单词字符:[^a-zA-z_0-9]

    常见对象(数量词)

    X? 一次或者一次也没有(只针对X所出现的次数) [abc]?

    X* 零次或者多次

    X+ 一次或者多次

    X{n} 恰好n次,长度就是n次,超过或者少于都不行

    X{n,} 至少n次

    X{n,m} 至少n次,但不超过m次

    正则表达式的分割功能

    String[] arr = String s.splite(String regex)

    案例

    String s = "97 33 36";

    String[] sArr = s.splite(" ");

    int[] array;

    for(int i =0;i<sArr.length();i++){

      array[i] = sArr[i].parseInt();

    }

    Arrays.sort();

    正则表达式的替换功能

    str.replaceAll(String regex,String replaceString);

    正则表达式Pattern的分组功能

    ((A)(B)(C))

    叠词正则表达式

    String regex = "(.)\\1(.)\\2"  \\1代表一组又出现一次 \\2代表第二组又出现一次

    str.replaceAll("(.)\\+","$1");  $1代表1组

    Pattern和Matcher的概述

    Pattern p = Pattern.compile(a*b); 将给定的正则表达式编译到模式中

    Matcher m = p.matcher("aaaaab"); 创建匹配给定输入的模式匹配器

    boolean b = m.matches(); 编译给定的正则表达式并给定输入与其匹配

    案例 把一个字符串中的手机号码获取出来

    String s = "我的手机号码13133337777";

    Pattern p = Pattern.compile("1[3578]\\d{9}");

    Matcher m = p.matcher(p);

    //boolean b = m.find();if(b){string str = m.group;}

    while(m.find()){

    string str = m.group;

    }

    二、Math类的概述和使用方法

      abs(int a) 绝对值

      ceil(double a)向上取整 结果仍然是double

      floor(double a)向下取整 结果仍然是double

      max(int a,int b) 去两个数中的最大值

      pow(double a,double b)前面是底数,后面是指数

      random() 随机数

      round(float a) 四舍五入的方法

      sqrt(double a) 开方操作

    Random类的使用

    Random r = new Random();

    int x = r.nextInt();

    Random r = new Random(Long类型的随机种子);

    int x = r.nextInt(100)+1;[1,100]

    三、System类中包含了有用的类字段和使用方法

    System 类包含一些有用的类字段和方法,不能被实例化

    public static void gc() 运行垃圾回收器

    finalize()不运行是可以调用gc()

    public static void exit(int status)终止当前运行的Java虚拟机。 该参数作为状态代码; 按照惯例,非零状态码表示异常终止。

    System.exit(0)正常退出java虚拟机

    public static long currentTimeMillis()返回当前时间(以毫秒为单位)。 请注意,虽然返回值的时间单位为毫秒,但该值的粒度取决于底层操作系统,并且可能较大。 例如,许多操作系统以几十毫秒为单位测量时间。

    结果

    在1970年1月1日UTC之间的当前时间和午夜之间的差异,以毫秒为单位。

    public static void arraycopy(Object src, int srcPos, Object dest,int destPos, int length)

    将指定源数组中的数组从指定位置复制到目标数组的指定位置。 阵列组件的一个子序列被从通过引用的源阵列复制src被引用的目标阵列dest 。 复制的组件数量等于length参数。 源阵列中位置srcPos至srcPos+length-1的组件分别复制到目标阵列的位置destPos至destPos+length-1 。

    四、BigInterger 存储超大的整数

    BigInteger bi1 = new BigInteger("100");

    public BigInteger[] divideAndRemainder(BigInteger val)返回两个BigInteger的数组,其中包含 (this / val)后跟 (this % val) 。

    五、BigDecimal 更精确的存储小数

    小数的加减乘除 要用字符串

    BigDecimal bd1 = new BigDecimal("2.0");

    BigDecimal bd2 = new BigDecimal("1.1");

    bd1.subtract(bd2);

    BigDecimal bd1 = BigDecimal.valueOf(2.0);

    BigDecimal bd2 = BigDecimal.valueOf(1.0);

    bd1.subtract(bd2);

    六、Date类的概述

    Date date = new Date();

    date.getTime() == System.currentTimeMillis();

    Date date2 = new Date(0);//日期从1970年开始

    date2.setTime()设置毫秒值 该百年时间对象

    七、SimpleDateFormat类

    DateFormat可以当作一个引用

    DateFormat df1 = DateFormat.getDateInstance();

    DateFormat df1 = new SimpleDateFormat();

    ------------------------------------------

    使用SimpleDateFormat创建即可

    Date date = new Date();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");

    String dateStr  = sdf.format(date);

    将时间字符串转换成日期对象

    String str = "xxx年xx月xx日xx:xx:xx";

    SimpleDateFormat sdf = new SimepleDateFormat("yyyy年MM月dd日HH:mm:ss");

    Date d = sdf.parse(str);

    案例从出生当现在多少天

    String birthday = "1995年08月02日";

    String today ="2019年1月19日";

    Date birthDate = new Date();

    SimpleDateFormat sdf = new SimepleDateFormat("yyyy年MM月dd日");

    birthDate = sdf.parse(birthday);

    Date todayDate = new Date();

    todayDate = sdf.parsetoday;

    long time = ((todayDate.getTime()-birthDate ())/1000/60/60/24);

    八、Calender类

    Calender calender = Calender.getInstance(); 注意 月份要自动加一

    Calender有常量值,其中代表的有年月日等

    calender.get(Calender.DAY_OF_MONTH);

    Calendar的add()和set()方法

    calender.add(Calender.YEAR,加减整数);

    calender.set(Calender.YEAR,2000);

    calender.set(2000,8(注意月份少一),2);

    相关文章

      网友评论

          本文标题:Java基础笔记总结(8)-常见类(2)正则表达式(Patter

          本文链接:https://www.haomeiwen.com/subject/didtjqtx.html