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");
}
}
}
}