有关正则表达式的简单概述和练习

正则表达式:表示字符串的判断条件

正则表达式的一般规则:

      [a-zA-A0-9]
      [^abc]
      [0-9] <=>\d
       .   表示任意一个字符
      \\.   表示  .
      [0-9]{6}    在0-9中间任意取6个
      [0-9]{6,}     在0-9中间任意取6个以上
      [0-9]{6,8}     在0-9中间任意取6到8个
      [a-z]?    一个或没有
      [a-z]*    可有可无
      [a-z]+   一个或多个

1、判断字符串:  

 由3个字母组成
 第一个字母是a/b/c
 第二个字母是d/e/f/g
 第三个字母是x/y/z
 System.out.println(str.matches("[abc][defg][xyz]"));

 匹配由一个字母组成的字符串
 System.out.println(str.matches("[a-zA-Z]"));


 匹配由一个数字组成的字符串
 System.out.println(str.matches("[0-9]"));
 System.out.println(str.matches("\\d"));

 匹配由一个字符组成的字符串,但不是a/b/c
System.out.println(str.matches("[^abc]"));     // [^...] 表示除了这些字符

 匹配由a开头的由2个字符组成的字符串
. 表示通配符,可以匹配任意一个类型的字符
System.out.println(str.matches("a."));


 判断是否是一个 .
 \\. Java先转义为\. ,正则再转义为.
 System.out.println(str.matches("\\."));


 怎么匹配 \ "c:\\java"
 \\\\ Java先转义为\\ ,正则再转义为\
System.out.println(str.matches("\\\\"));

 

数量词

+ 表示之前的字符至少出现1次 >= 1

 System.out.println(str.matches("a.+"));


 匹配由小写字母开头由数字结尾的字符串
 * 表示之前的字符可有可无 >= 0
 System.out.println(str.matches("[a-z].*\\d"));


 匹配由a开头至多2个字符组成的字符串

 ? 表示之前的字符至多出现1次 <= 1
System.out.println(str.matches("a.?"));  a   ab

 匹配由5个小写字母组成的字符串
 {n} 表示之前的字符恰好出现n次 == n
 System.out.println(str.matches("[a-z]{5}"));acccc


 匹配至少由5个小写字母组成的字符串
 System.out.println(str.matches("[a-z]{5,}"));


匹配由8-12个小写字母组成的字符串
System.out.println(str.matches("[a-z]{8,12}"));

2、练习

     (1) 正则表达式:验证电话号码

public static void test1() {
		Scanner sc=new Scanner(System.in);
		while(true) {
			System.out.println("input phone:");
			String phone=sc.next();
			if(phone.matches("1[3456789][0-9]{9}")){
				System.out.println("正确:"+phone);
			}else{
				System.out.println("错误");
			}
		}
		
	}

  (2)邮箱的验证   合法邮箱:jichu@tedu.com.cn;123456@163.com;aa222@123.com.cn;aa222@123.com
           //[a-zA-Z0-9_]+@[a-z0-9]{2,}(\\.[a-z]{2,3})?{1,2}

public static void test2() {
		Scanner sc=new Scanner(System.in);
		System.out.println("input email:");
		String email=sc.next();
		String regax="[a-zA-Z0-9_]+@[a-z0-9]{2,}(\\.[a-z]{2,3}){1,2}?";
		if(email.matches(regax)) {
			System.out.println("正确,邮箱为:"+email);
		}else {
			System.err.println("邮箱格式错误!");
			
		}
		
	}

(3)输入一个字符串,判断其是否是一个小数字符串      合法小数:10.28  0.56   3.00  13.85  15.

public static void test3() {
		Scanner sc=new Scanner(System.in);
		System.out.println("input String:");
		String email=sc.next();
		String regax="[0]*[1-9](\\.?)[0-9]{2,}";
		String regax1="f";
		if(email.matches(regax1)) {
			System.out.println(email+"正确!");
		}else {
			System.err.println("格式错误!");
			
		}
		
	}

(4)检验密码8-20位,小写字母/大写字母/数字中的至少两种

public static void test4_1() {
		Scanner sc=new Scanner(System.in);
		System.out.println("input pwd:");
		String pwd=sc.next();
		if(!pwd.matches("[a-zA-Z0-9]{8-20}")) {
			System.err.println("密码错误!");
			return;			
		}
		int count=0;
		if(pwd.matches(".*[a-z]+.*")) {
			count++;
		}
		if(pwd.matches(".*[A-Z]+.*")) {
			count++;
		}
		if(pwd.matches(".*[0-9]+.*")) {
			count++;
		}
		if(count>=2) {
			System.out.println("密码:"+pwd);
		}else {
			System.err.println("密码格式错误!");
		}
	}
public static void test4() {
		Scanner sc=new Scanner(System.in);
		System.out.println("input pwd:");
		String pwd=sc.next();
		String str="[a-zA-Z0-9]{8,20}";
		int a=0,b=0,d=0;
		for(int i=0;i<str.length();i++) {
			char c=str.charAt(i);
			if(c>='a'&&c<='z') {
				a=1;			
			}else if(c>='0'&&c<='9') {
				b=1;
			}else if(c>='A'&c<='Z') {
				d=1;
			}			
		}
		if(a+b+d>=2&&pwd.matches(str)) {
			System.out.println(pwd+"正确!");
		}else {
			System.err.println("格式错误");
		}
		

	}

(5)//"a b c"   \\s   ""   去掉字符中的空格

public static void test5() {
		String str="a b c";
		String str2="abcqsbabcqsb";
		System.out.println(str.replaceAll("\\s", "*"));
		System.out.println(str2.replaceAll("qsb", "*"));
	}

(6)提取信息“http://127.0.0.1:8080/myweb/imdex.html?name=admin&pw=123456

public static void test7() {
		String url="http://127.0.0.1:8080/myweb/imdex.html?name=admin&pw=123456";
		int index=url.indexOf("?");
		if(index==-1) {
			System.out.println("没有可以解析的参数");
		}else {
			String params=url.substring(index+1);
			String[] paramArray=params.split("&");
			for(String values:paramArray) {
				String[] mapArray=values.split("=");
				System.out.println(mapArray[0]+":"+mapArray[1]);
			}
		}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值