正则表达式学习笔记

 1、概念:

正则表达式是一个字符串,用来面试匹配一个字符串集合的模式,可以用来匹配、替换和拆分字符串。

2、正则表达式规则:

3、用matches匹配字符样例:

"Java is fun".matches("Java.* ")

"Java234455".matches("Java[\\d]* ")

"Jaaavaaaaa".matches("Ja{2,9}va{4,}"

4、正则表达式示例:

样例一、
package org.example;

import java.util.Scanner;

public class zhengze_try {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入社会安全号(xxx-xx-xxxx):");
        String str = input.nextLine();
        System.out.println(str.matches("[\\d]{3}-[\\d]{2}-[\\d]{4}"));
    }
}

样例二、

package org.example;

import java.util.Scanner;

public class zhengze_try {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your name:");
        String str = input.nextLine();
        System.out.println(str.matches("[A-Z][a-zA-Z]{1,24}"));
    }
}

5、替换和拆分字符:

(1)替换字符串:
1、replaceAll:
package org.example;

import java.util.Scanner;

public class zhengze_try {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter String: ");
        String str = input.nextLine();
        System.out.println(str.replaceAll("a\\w"," Java "));
    }
}

2、replaceFirst:
package org.example;

import java.util.Scanner;

public class zhengze_try {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter String: ");
        String str = input.nextLine();
        System.out.println(str.replaceFirst("a\\w"," Java "));
    }
}

(2)分割字符串:
split:

package org.example;

import java.util.Arrays;
import java.util.Scanner;

public class zhengze_try {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String str = input.nextLine();
        System.out.println(Arrays.toString(str.split("[\\d]",4)));
    }
}

6、用正则判断输入账号、密码是否正确:

package org.example;
import java.util.Scanner;

public class zhengze_try {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your username: ");
        String username = input.nextLine();
        System.out.print("Enter your password: ");
        String password = input.nextLine();
        boolean shuzi = false;
        boolean daxie = false;
        boolean xiaoxie = false;
        boolean len = false;
        boolean tail = false;
        len = username.matches(".{10,11}.*");
        tail = username.matches(".*@qq.com");
        shuzi = password.matches(".*[0-9].*");
        daxie = password.matches(".*[A-Z].*");
        xiaoxie = password.matches(".*[a-z].*");
        if(len && tail){
            System.out.println("your username is correct");
        }
        else{
            System.out.println("your username is incorrect");
            if(!len){
                System.out.println("your username's len is incorrect");
            }
            if(!tail){
                System.out.println("your username's tail is incorrect");
            }
        }
        if (shuzi && daxie && xiaoxie /*&& jiewei*/) {
            System.out.println("your password is correct");
        }
        else{
            System.out.println("your password is incorrect");
            if (!shuzi){
                System.out.println("your password don't have shuzi");
            }
            if (!daxie){
                System.out.println("your password don't have daxie");
            }
            if (!xiaoxie){
                System.out.println("your password don't have xiaoxie");
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值