JAVASE基础模块十九(正则表达式)
- 传统方法校验qq号
* 校验qq号
* 5-15位数字
* 0 不能开头
import java.util.Scanner;
public class Zheng {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("输入qq号");
String qqNumber = s.nextLine();
boolean f=checkNumber(qqNumber);
PanDuan(f);
}
private static boolean checkNumber(String qqNumber) {
boolean flag=false;
if(qqNumber.length()>=5&&qqNumber.length() <= 15&&!qqNumber.startsWith("0")){
for (int i = 0; i < qqNumber.length(); i++) {
char cc=qqNumber.charAt(i);
if(Character.isDigit(cc)){
flag=true;
break;
}else {
flag=false;
}
}
}
return flag;
}
}
输入qq号
0258
格式错误
Process finished with exit code 0
- 传统方法校验手机号
import java.util.Scanner;
public class ShouJi {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("输入号");
String qqNumber = s.nextLine();
boolean f=checkNumber(qqNumber);
PanDuan(f);
}
private static void PanDuan(boolean f) {
if(f==false){
System.out.println("格式错误");
}else {
System.out.println("d");
}
}
private static boolean checkNumber(String qqNumber) {
boolean flag=false;
if(qqNumber.length()==11&&qqNumber.startsWith("13")||qqNumber.startsWith("15")||qqNumber.startsWith("17")||qqNumber.startsWith("18")){
for (int i = 2; i < qqNumber.length(); i++) {
char cc=qqNumber.charAt(i);
if(Character.isDigit(cc)){
flag=true;
}else {
flag=false; break;
}
}}
return flag;
}
}
运行结果:
输入号
13546464646
d
Process finished with exit code 0
- 传统方法校验邮箱
import java.util.Scanner;
public class YouXianng {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("输入号");
String you = s.nextLine();
boolean f = checkN(you);
PanDuan(f);
}
private static boolean checkN(String w) {
boolean f = false;
if (w.length() >= 14 && w.length() <= 26 && w.endsWith("@163.com")) {
if (Character.isLetter(w.charAt(0))) {
for (int i = 1; i < w.length() - 8; i++) {
char cc = w.charAt(i);
if (Character.isDigit(cc) || Character.isLetter(cc) || cc == '_') {
f = true;
} else {
f = false;
break;
}
}
}
}
return f;
}
private static void PanDuan(boolean f) {
if (f == false) {
System.out.println("格式错误");
} else {
System.out.println("格式正确");
}
}
}
运行结果:
输入号
ss55gg__pp@163.com
格式正确
Process finished with exit code 0
正则表达式
public class ZhengZe {
public static void main(String[] args) {
//定义正则表达式
String regx = "a";
String regx0 = "[a,b,c]";//是列举的某一个
String regxf = "[a-z]";
String regxx = "[a-zA-Z0-9]";
String regx1 = "[0-9]";
String regx2 = "[^0-9]";//^不是列举的某一个
String regx4 = ".";//配置任意单个字符
String regx3 = "\\.";//转义 \转义字符
String regx5 = "|";//| 或者
String regx6 = "\\|";//转义 | 它本身
String regx7 = "\\d";//代表[0-9] 是0-9
String regx8 = "\\D";//代表^[0-9] 不是0-9
String regx9 = "\\w";//代表[a-zA-Z_0-9]
String regx11 = "\\W";//代表^[a-zA-Z_0-9]
String regx12 = " ";//代表空格
String regx13 = "\\s";//也代表空格
String regx14 = "\\S";//代表^空格 不是空格
String regx15 = "abc$";//以abc整体结尾
String regx16 = "^abc";//以abc整体开头
String regx17 = "[0-9]+";//[0-9]+ 可以出现一个或多个
String regx18 = "[a-z]*";//[a-z]* 可以出现零个或多个 一个也算多个 空串算零个
String regx19 = "[A-Z]?"; //? 一个或零个
String regx20 = "\\w+"; //? 一个或零个
String regx2l = "[a-zA-Z0-9_]+"; //? 一个或零个
String regx21 = "[a-z]{6}";//正好几个
String regx22 = "[a-zA-Z]{6,}";//至少几个
String regx23 = "[a-zA-Z]{6,16}";//6-16个
//区分 System.out.println("abc".equals(regx));
System.out.println("A".matches(regx));
System.out.println("d".matches(regx));
System.out.println("Aa".matches(regx));
System.out.println("2".matches(regx1));
System.out.println("2".matches(regx2));
}
}
- qq号
String regex="[1-9][0-9]{4,14}";
- 手机号
tring regex="[1][3,5,7,8][0-9]{9}";
-
邮箱
String regex="[a-zA-Z][a-zA-Z0-9_]{5,17}@163.com$";
-
v.split("\+"); 根据正则切割字符串
import java.util.Arrays; public class Ty { public static void main(String[] args) { String v="半缘修道半缘君+衣带渐宽终不悔+惊起一滩鸥鹭"; System.out.println(v.substring(0, v.indexOf("+"))); System.out.println(v.substring(v.indexOf("+")+1, v.lastIndexOf("+"))); System.out.println(v.substring(v.lastIndexOf("+")+1)); //根据正则来切割这个字符串 返回的是一个字符串数组 String[] s=v.split("\\+"); System.out.println(Arrays.toString(s)); } }
-
排序实例
import java.util.Arrays;
public class Gh {
public static void main(String[] args) {
String v = "15 69 85 44 7 569 22 10 23 66 99";
String[] s = v.split("\\s+");
System.out.println(Arrays.toString(s));
int[] a = new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i] = Integer.valueOf(s[i]);
}
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
String ss = new String();
for (int i = 0; i < a.length; i++) {
ss += a[i] + " ";
}
System.out.println(ss);
}
}
运行结果:
[15, 69, 85, 44, 7, 569, 22, 10, 23, 66, 99]
[15, 69, 85, 44, 7, 569, 22, 10, 23, 66, 99]
[7, 10, 15, 22, 23, 44, 66, 69, 85, 99, 569]
7 10 15 22 23 44 66 69 85 99 569
进程已结束,退出代码0
- replaceAll()
public class Rep {
public static void main(String[] args) {
String s="456aw489dwa半fiuaefhi缘fahf" +
"ua修道半fahuaf缘fehaf君,gusfeegfyue" +
"12356efae衣af带fae渐456宽45终af不78" +
"9悔,feafaefaedfaq惊起fw" +
"ab一fddfvd滩48987653鸥鹭";
//根据正则表达式去替换
System.out.println(s.replaceAll("[a-zA-Z0-9]+", ""));
}
}
运行结果:
半缘修道半缘君,衣带渐宽终不悔,惊起一滩鸥鹭
进程已结束,退出代码0
- 匹配器 Matcher与模式器 Pattern
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PiPeiQi {
public static void main(String[] args) {
//匹配器 Matcher
//模式器 Pattern
//把一个正则表达式封装到模式器里面
Pattern p=Pattern.compile("a*b");
//调用模式器中的matcher("aaaaaaab");方法 传入一个待匹配的数据
Matcher m = p.matcher("aaaaaaab");
//调用匹配器中的匹配方法 看数据是否匹配正确
System.out.println(m.matches());
//如果需求只是看一个数据符不符合一个正则 只需要调用string 类中的matches("a*b")
System.out.println("aaaaaaab".matches("a*b"));
}
}
运行结果:
true
true
进程已结束,退出代码0
-
matcher.find() matcher.group() 可以一直查找下去 并输出上次查找到的结果
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Pm { public static void main(String[] args) { String d="dg duo gduwa hsefh hvjh bao huisah hvsd bao haud bjds wob axx h sh ia csvb jzvc "; String regx="\\b[a-z]{3}\\b"; Pattern pattern = Pattern.compile(regx); Matcher matcher = pattern.matcher(d); System.out.println(); //可以一直查找下去 并输出上次查找到的结果 while (matcher.find()) { String s=matcher.group(); System.out.print(s+"\t"); } } } 运行结果: duo bao bao wob axx 进程已结束,退出代码0