正则表达式

目录

1.含义

2.表达式实例与注意点

实例1:字符串匹配问题

        [表达式]--判断字符串与表达式内容是否匹配

        [^表达式]--判断字符串与非表达式内容是否匹配

实例2:表达式执行次数问题

 实例3:表达式间或的问题

​ 实例4:找出字符串中的中文和大写字母

3.域名解析

 4.学生信息录入


1.含义

        正则表达式描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

2.表达式实例与注意点

        正则表达式的组件可以是单个的字符、字符集合、字符范围、字符间的选择或者所有这些组件的任意组合。

实例1:字符串匹配问题

        [表达式]--判断字符串与表达式内容是否匹配

public class RegexDemo01 {
	public static void main(String[] args) {
		String str = "g9H";
		String Regex = "[a-z][0-9][A-H]";
		
		System.out.println(str.matches(Regex));//matches去匹配
	}
}

        [^表达式]--判断字符串与非表达式内容是否匹配

public class RegexDemo03 {
	public static void main(String[] args) {
		String str = "$%^";
		String regex = "[^0-9][^A-Z][^a-z]";//注意^的位置
		
		System.out.println(str.matches(regex));
	}
}

实例2:表达式执行次数问题

  • [表达式]{n}    表达式执行n次
  • [表达式]{n, }    表达式至少执行n次
  • [表达式]{m,n}    表达式执行m-n次
  • [表达式]+    表达式执行1次或多次
  • [表达式]*    表达式执行0次或多次
  • [表达式]?    表达式执行0次或1次
public class RegexDemo04 {
	public static void main(String[] args) {
		
		//6位数字验证
		String num = "765439";
		String regex = "[0-9]{6}";
		System.out.println(num.matches(regex));
		
		//电话号码验证
		String tellphone = "18979564533";
		String regex1 = "[1][0-9]{10}";//[]{}仅靠在一起的
		System.out.println(tellphone.matches(regex1));
		
		//身份证验证
		String iditityNum = "412987200811279878";
		String regex2 = "[1-9][0-9]{16}[0-9X]";
		System.out.println(iditityNum.matches(regex2));
	}
}

public class RegexDemo05 {
	public static void main(String[] args) {
		String str = "ni0yf6Mjiy6459";
		System.out.println(str.length());
		//重复执行至少8次
		String regex = "[0-9a-zA-Z]{8,}";//至少执行次数>str.length()
		System.out.println(str.matches(regex));
		
		//重复执行3-14次
		String regex1 = "[0-9a-zA-Z]{3,14}";//最大执行次数=<str.length(),否则结果为false
		System.out.println(str.matches(regex1));
	}
}

public class RegexDemo06 {
	public static void main(String[] args) {
		String str = "09876543234566";
		
		//1.+  执行一次或多次
		String regex = "[0-9]+";
		System.out.println(str.matches(regex));
		
		//2.\\d表示数字字符
		String regex1 = "\\d+";
		System.out.println(str.matches(regex1));
		
		//3.*  执行零次或多次
		String regex2 = "[0-9]*";
		System.out.println(str.matches(regex2));
		
		String regex3 = "\\d*";
		System.out.println(str.matches(regex3));
		
		//4.?  执行0次或1次
		String regex4 = "[0-9]?";
		System.out.println(str.matches(regex4));
		
		String str1 = "6";
		String regex5 = "\\d?";
		System.out.println(str1.matches(regex5));
	}
}

 实例3:表达式间或的问题

        [表达式]|[表达式]

public class RegexDemo07 {
	public static void main(String[] args) {
		String str = "保密";
		String regex = "[男]|[女]|(保密)";//性别是:  男或女或保密
		
		System.out.println(str.matches(regex));
    }
}

实例4:找出字符串中的中文和大写字母

public class RegexDemo08 {
	public static void main(String[] args) {
		
		String str = "23gh平凡89njas世POOP界oqpybJF";
		//1.找出字符串中的中文--使用replaceall()将非中文变成""
		String regex = "[^\u4e00-\u9fa5]";
		System.out.println(str.replaceAll(regex, ""));
		
		//2.找出大写字母
		String regex1 = "[^A-Z]";
		System.out.println(str.replaceAll(regex1, ""));
	}
}

3.域名解析

public class RegexDemo02 {
	public static void main(String[] args) {
		String url = "http://dyit:8080/pets?username=blank&password=684356";
		//解析  用户名   密码
		String[] strs = url.split("[?]");
		for (String sx : strs) {
//			System.out.println(sx);
		}
		
		String[] t = url.split("[&]");
		for (String tx : t) {
//			System.out.println(tx);
		}
		
		System.out.println("用户名:"+t[0].split("[=]")[1]);
		System.out.println("密码:"+t[1].split("[=]")[1]);
	}
}

 4.学生信息录入

public class practise01 {
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		String[][] students = new String[5][5];
		InputUsername();
		InputName();
		InputYear();
		InputAge();
		InputSex();
	}

	//1.用户名
	public static void InputUsername() {
		boolean f = true;
		while (f) {
			System.out.print("请输入用户名:");
			String username = sc.nextLine();
			if(username.length()<20) {
				f = false;
			}else {
				System.out.println("抱歉,您输入的用户名过长...");
			}
		}
	}
	
	//2.姓名
	public static void InputName() {
		String regex = "[\u4e00-\u9fa5]{2,}";
		boolean f = true;
		while(f) {
			System.out.print("请输入姓名:");
			String name = sc.nextLine();
			if(name.matches(regex)) {
				f = false;
			}else {
				System.out.println("姓名只能为2个字以上的姓氏!");
			}
		}
	}
	
	//3.出生年月日
	public static void InputYear() {
		boolean f = true;
		String regex = "(1[8-9][0-9]{2}|2[0-1][0-9]{2})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])";
		/**
		 * 年 :1[8-9][0-9]{2}|2[0-1][0-9]{2}    月:0[1-9]|1[0-2]    
		 * 日:0[1-9]|[1-2][0-9]|3[0-1]
		 */
		while(f) {
			System.out.print("请输入年-月-日(XXXX-XX-XX):");
			String Year = sc.nextLine();
			if(Year.matches(regex)) {
				f = false;
			}else {
				System.out.println("有误....");
			}
		}
	}
	
	//4.年龄   18以上100一下
	public static void InputAge() {
		boolean f = true;
		String regex = "1[8-9]|[2-9][0-9]";
		while(f) {
			System.out.print("请输入您的年龄[18-99岁]:");
			String age = sc.nextLine();
			if(age.matches(regex)) {
				f = false;
			}else {
				System.out.println("有误...");
			}
		}
	}
	
	//5.性别
	public static void InputSex() {
		boolean f = true;
		String regex = "[男]|[女]|(保密)";
		while(f) {
			System.out.print("请输入您的性别:");
			String age = sc.nextLine();
			if(age.matches(regex)) {
				f = false;
			}else {
				System.out.println("有误...");
			}
		}
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值