Java正则校验

使用正则完成验证
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "123";					// 定义字符串
		if (str.matches("\\d+")) {				// 正则验证
			System.out.println("是由数字所组成!");
		} else {
			System.out.println("不是由数字所组成!");
		}
	}
}


将字符串中的字母消除
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "a1bb2ccc3dddd4eeeee5fffffff6ggggggg7";
		String regex = "[a-zA-Z]+";				// 字母出现1次或多次
		System.out.println(str.replaceAll(regex, ""));		// 全部替换
		System.out.println(str.replaceFirst(regex, ""));	// 替换首个
	}
}


字符串按照数字拆分
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "a1bb2ccc3dddd4eeeee5fffffff6ggggggg7";
		String regex = "\\d";					// 数字出现1次,[0-9]
		String result[] = str.split(regex);			// 拆分
		for (int x = 0; x < result.length; x++) {
			System.out.print(result[x] + "、");
		}
	}
}


做一个简单的验证,要求字符串格式“aaaaab”,在b之前可以有无数多个a,但是不能没有,至少1个。
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "aaaaaab";
		String regex = "a+b";			// 1个或多个字母a和1个字母b
		System.out.println(str.matches(regex));	// 正则验证
	}
}


验证一个字符串是否是整型数据,如果是则将其变为int型数据,而后执行乘法操作
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "123";				// 定义字符串
		String regex = "\\d+";				// 是否为数字
		if (str.matches(regex)) { 			// 符合于验证要求
			int data = Integer.parseInt(str); 	// 字符串变为int型数据
			System.out.println(data * data);
		} else {
			System.out.println("字符串不是数字所组成!");
		}
	}
}


验证字符串是否是小数,如果是则将其变为double型数据,而后执行乘法操作
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "123.1";
		String regex = "\\d+(\\.\\d+)?";
		if (str.matches(regex)) { 				// 符合于验证要求
			double data = Double.parseDouble(str); 		// 字符串变为double型数据
			System.out.println(data * data);
		} else {
			System.out.println("字符串不是数字所组成!");
		}
	}
}


输入一个字符串,按照年-月-日 时-分-秒的形式,如果正确,则将其变为Date型数据
package com.bank.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "1998-09-19 10:10:10.100";	// 字符串
		String regex = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}";
		if (str.matches(regex)) { // 符合于验证要求
			Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
					.parse(str);
			System.out.println(date);
		} else {
			System.out.println("字符串不是日期!");
		}
	}
}


一个用户名只能由字母、数字、_所组成,其长度只能是6~15位
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "343234FDSF_";
		String regex = "\\w{6,15}";
		if (str.matches(regex)) { // 符合于验证要求
			System.out.println("用户名合法。");
		} else {
			System.out.println("用户名非法!");
		}
	}
}

现在编写一个正则表达式要求验证电话号码是否正确。
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "51283346";
		// 第一步:\\d{7,8},因为电话号码可能是7~8位所组成;
		// 第二步:(\\d{3,4})?\\d{7,8},因为区号由3~4位所组成;
		// 第三步:(\\d{3,4})?-?\\d{7,8},因为-可能出现也可能不出现
		// 第四步:((\\d{3,4}|\\(\\d{3,4}\\))-?)?\\d{7,8}
		String regex = "((\\d{3,4}|\\(\\d{3,4}\\))-?)?\\d{7,8}";
		if (str.matches(regex)) { // 符合于验证要求
			System.out.println("TRUE,电话号码输入合法。");
		} else {
			System.out.println("FLASE,电话号码输入非法!");
		}
	}
}


现在要求验证一个email地址,email地址的用户名由字母、数字、_、.所组成,其中不能以数字和.开头,而且email地址的域名只能是.com、.cn、.net
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "mldnqa@163.net";
		String regex = "[a-zA-Z_][a-zA-Z_0-9\\.]*@[a-zA-Z_0-9\\.]+\\.(com|cn|net)";
		if (str.matches(regex)) { 							// 符合于验证要求
			System.out.println("TRUE,EMAIL输入合法。");
		} else {
			System.out.println("FLASE,EMAIL输入非法!");
		}
	}
}

验证一个email地址,
package com.bank.demo;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class TestDemo {
    public static void main(String[] args){
        String str="1076268865@qq.com";
        String pat="\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
        Pattern p=Pattern.compile(pat);
        Matcher m=p.matcher(str);
        if(m.matches()){
            System.out.println("邮箱正确!");
        }else{
            System.out.println("邮箱不正确!");
        }
    }
}


现在判断一个字符串的组成,其组成原则“姓名:年龄:成绩|姓名:年龄:成绩|姓名:年龄:成绩|…”

写出来验证的正则表达式
package com.bank.demo;
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "SMITH:19:90.1|ALLEN:18:90.1|KING:20:95.2";
		String regex = "([a-zA-Z]+:\\d{1,3}:\\d{1,3}(\\.\\d{1,2})?\\|)+"
				+ "([a-zA-Z]+:\\d{1,3}:\\d{1,3}(\\.\\d{1,2})?)?";
		if (str.matches(regex)) { 						// 符合于验证要求
			System.out.println("TRUE,输入合法。");
		} else {
			System.out.println("FLASE,输入非法!");
		}
	}
}

将所有的数据排序,按照成绩由高到低排序,如果成绩相同,则按照年龄由低到高排序
package com.bank.demo;
import java.util.Arrays;
class Student implements Comparable<Student> {		// 实现比较器接口
	private String name;
	private int age;
	private double score;
	public Student(String name, int age, double score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", score=" + score
				+ "]\n";
	}
	@Override
	public int compareTo(Student o) {
		if (this.score > o.score) {
			return -1;
		} else if (this.score < o.score) {
			return 1;
		} else {
			if (this.age > o.age) {
				return 1;
			} else if (this.age < o.age) {
				return -1;
			} else {
				return 0;
			}
		}
	}
}
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "SMITH:19:90.1|ALLEN:18:90.1|KING:20:95.2";
		String regex = "([a-zA-Z]+:\\d{1,3}:\\d{1,3}(\\.\\d{1,2})?\\|)+"
				+ "([a-zA-Z]+:\\d{1,3}:\\d{1,3}(\\.\\d{1,2})?)?";
		if (str.matches(regex)) { 				// 符合于验证要求
			String result[] = str.split("\\|"); 		// 按正则拆分
			Student stu[] = new Student[result.length]; // 此时的数组大小就是对象数组大小
			for (int x = 0; x < result.length; x++) {
				String temp[] = result[x].split(":");
				stu[x] = new Student(temp[0], Integer.parseInt(temp[1]),
						Double.parseDouble(temp[2]));
			}
			Arrays.sort(stu);				// 数组排序
			System.out.println(Arrays.toString(stu));
		} else {
			System.out.println("FLASE,输入非法!");
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值