首页 > 其他分享 >包装类 正则表达式

包装类 正则表达式

时间:2022-11-20 17:25:10浏览次数:30  
标签:String 正则表达式 matches System 包装 println public out

包装类

package com.api.integer;
//包装类
public class Test {
    public static void main(String[] args) {
        int a = 10;
        Integer b = 11;
        System.out.println(a);
        System.out.println(b);
        System.out.println("====================");

        Integer c = a;
        Integer d = 100;
        int e = d;
        System.out.println(c);
        System.out.println(e);

        double f = 99.5;
        Double g = f;
        double h = g;
        System.out.println(h);
        System.out.println("===========================================");

        //int a1 = null; null is for reference type
        Integer a2 = null;
        Integer a3 = 0;
        System.out.println(a2);
        System.out.println("==========================================");

        Integer b1 = 23;
        String s = b1.toString();
        System.out.println(s + 1);

        String s1 = Integer.toString(b1);
        System.out.println(s1 + 1);

        //可以直接+字符串得到字符串类型
        String s2 = b1 + "";
        System.out.println(s2 + 1);
        System.out.println("====================================================");

        String m = "23";
        int n = Integer.parseInt(m);
        System.out.println(n+1);
        int n2 = Integer.valueOf(m);
        System.out.println(n2+1);

        String m1 = "99.9";
        double n1 = Double.parseDouble(m1);
        System.out.println(n1 + 0.1);
        double n3 = Double.valueOf(m1);
        System.out.println(n3+0.1);
    }
}

正则表达式

package com.api.regex;
//a sharp contrast
public class RegexDemo01 {
    public static void main(String[] args) {
        System.out.println(checkQQ("766845135"));
        System.out.println(checkQQ("123456"));
        System.out.println(checkQQ("a1231233"));
        System.out.println(checkQQ(null));
        System.out.println(checkQQ("12345"));

        System.out.println(check2("766845135"));
        System.out.println(check2("123456"));
        System.out.println(check2("a1231233"));
        System.out.println(check2(null));
        System.out.println(check2("12345"));

    }

    public static boolean check2(String qq) {
        return qq != null && qq.matches("\\d{6,20}");
    }
    public static boolean checkQQ(String qq){
        if (qq == null || qq.length() < 6 || qq.length() > 20 ){
            return false;
        }
        for (int i = 0; i < qq.length(); i++) {
            char a = qq.charAt(i);
            if (a < '0' || a > '9'){
                return false;
            }
        }
        return true;
    }
}

package com.api.regex;
//regex rule
public class RegexDemo02 {
    public static void main(String[] args) {
        //java.util.regex.Pattern
        System.out.println("z".matches("[abc]"));
        System.out.println("a".matches("[abc]"));

        System.out.println("z".matches("[^abc]"));
        System.out.println("a".matches("[^abc]"));

        System.out.println("aa".matches("[abc]"));
        System.out.println("aa".matches("[abc]+"));
        System.out.println("=============================================");

        System.out.println("2".matches("\\d"));
        System.out.println("a".matches("\\d"));
        System.out.println("222".matches("\\d"));
        System.out.println("2".matches("\\D"));
        System.out.println("a".matches("\\D"));
        System.out.println("===============================================");
        System.out.println("a".matches("\\w"));
        System.out.println("2".matches("\\w"));
        System.out.println("_".matches("\\w"));
        System.out.println("aa".matches("\\w"));
        System.out.println("你".matches("\\w"));
        System.out.println("你".matches("\\W"));
        System.out.println("==============================================");

        System.out.println("2442fsfs".matches("\\w{6,}"));
        System.out.println("2412f".matches("\\w{6,}"));
        System.out.println("===============================================");

        System.out.println("35as".matches("[a-zA-Z0-9]{4,}"));
        System.out.println("23_F".matches("[a-zA-Z0-9]{4,}"));
        System.out.println("23dF".matches("[\\w&&[^_]]{4,}"));
        System.out.println("23_F".matches("\\w&&[^_]{4,}"));
    }
}
package com.api.regex;
//案例:校验输入的信息
import java.util.Scanner;

public class RegexTest3 {
    public static void main(String[] args) {
        //checkPhone();
        checkMoney();
    }

    public static void checkMoney() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入您的金额:");
            String phone = scan.next();
            //[](只匹配一个字符) 和 ()的区别          && 取交集
            if (phone != null && phone.matches("\\d+(\\.\\d+)?")){

                System.out.println("输入正确");
                break;
            }else {
                System.out.println("输入错误");
            }
        }
        scan.close();
    }
    public static void checkTel() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入您的注册tel号码:");
            String phone = scan.next();
            //027-3572457       0273572457
            if (phone != null && phone.matches("0\\d{2,6}-?\\d{5,20}")){
                System.out.println("输入正确");
                break;
            }else {
                System.out.println("输入错误");
            }
        }
        scan.close();
    }
    public static void checkPhone() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入您的注册手机号码:");
            String phone = scan.next();
            if (phone != null && phone.matches("1[3-9]\\d{9}")){
                System.out.println("输入正确");
                break;
            }else {
                System.out.println("输入错误");
            }
        }
        scan.close();
    }
    public static void checkEmail() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            //[email protected]
            //[email protected]
            //[email protected]
            System.out.println("请您输入您的注册Email:");
            String phone = scan.next();
            if (phone != null && phone.matches("\\w{1,30}@[a-zA-Z1-9]{2,20}" +
                    "(\\.[a-zA-Z1-9]{2,20}){1,2}")){
                System.out.println("输入正确");
                break;
            }else {
                System.out.println("输入错误");
            }
        }
        scan.close();
    }
}
package com.api.regex;
//正则表达式在字符串方法中的使用
import java.util.Arrays;

public class RegexDemo04 {
    public static void main(String[] args) {
        String names = "小明asdasd3132131小张asdddd1115小林";

        String[] s1 = names.split("\\w+");
        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
        System.out.println(Arrays.toString(s1));


        String s2 = names.replaceAll("\\w+", " ");
        System.out.println(s2);
    }
}
package com.api.regex;
//正则表达式爬取信息
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo05 {
    public static void main(String[] args) {
        String s = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" +
                "[email protected],电话18762832633,0203232323 " +
                "邮箱[email protected], 400-100-3233, 4001003232";

        String regex = "(0\\d{2,6}-?\\d{5,20})|(1[3-9]\\d{9})" +
                "|(\\w{1,30}@[a-zA-Z1-9]{2,20}(\\.[a-zA-Z1-9]{2,20}){1,2})" +
                "|(400-?\\d{3,9}-?\\d{3,9})";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);

        while (matcher.find()){
            System.out.println(matcher.group());
        }
    }
}

标签:String,正则表达式,matches,System,包装,println,public,out
From: https://www.cnblogs.com/799rijiyuelei/p/16908952.html

相关文章

  • 【正则表达式 】常见密码正则表达式
    第一种至少8-16个字符,至少1个大写字母,1个小写字母和1个数字,其他可以是任意字符: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\s\S]{8,......
  • 【正则匹配】正则表达式的先行断言(lookahead)和后行断言(lookbehind)
    原文:https://www.runoob.com/w3cnote/reg-lookahead-lookbehind.html正则表达式的先行断言和后行断言简述(?=pattern)正向先行断言(?!pattern)负向先行断言(?<=pat......
  • java 正则表达式讲解
    比如:判断字符串中不能含有“,:*”三个字符java写法:Stringstr="*aaa";Stringregex="^.*[,:*].*$";booleanb=str.matches(regex);=====......
  • 使用Regex正则表达式替换.txt文本文件中指定的词
     1///<summary>2///替换文本文件中的词3///</summary>4///<paramname="filePath"></param>5///<paramn......
  • 18.正则表达式
    正则表达式认识正则正则表达式,又称规则表达式,(RegularExpression,在代码中常简写为regex、regexp或RE),是一种文本模式,包括普通字符(例如,a到z之间的字母)和特殊字符(称为"......
  • js当replace替换的字符是正则表达式时如:$
    今天做项目碰到一个问题:将'姓名:${name}性别:${sex}'  转化为'姓名:张三性别:男',只是用repace的时候是没有问题的如:letstr='姓名:${name}性别:${sex}'newStr=......
  • 常用数字与字母的正则表达式
    参考:https://www.cnblogs.com/xuqiang7/p/11082729.html+验证数字的正则表达式集+验证数字:^[0-9]*$+验证n位的数字:^\d{n}$+验证至少n位数字:^\d{n,}$+验证m-n位......
  • Java——包装类详解
                 装箱与拆箱的概念.基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:装箱:从基本类型转换为对应的包装......
  • 正则表达式基本语法的详解
    正则表达式基本语法的详解本文给给大家介绍正则表达式的基本语法,需要的朋友可以参考下 正则表达式是一种文本模式,包括普通字符(例如,a到z之间的字母)和特殊字符(称为“......
  • 正则表达式中的预搜索
    格式:1.正向预搜索,右侧是或者不是什么:"(?=xxxxx)","(?!xxxxx)";2.反向预搜索,左侧是或者不是什么:"(?<=xxxxx)","(?<!xxxxx)"注意:1.括号是必须的且不参与反向引......