正则表达式是对字符串操作的一种逻辑公式就是用事先定义好的一些特定字符、及这些特定字符的组合组成一个“规则字符串”这个“规则字符串”用来表达对字符串的一种过滤逻辑
预定义字符类
.任意字符
\d数字0-9
\D非数字
\s空白字符
\S非空白字符
\w单词字符
\W非单词字符
范围词
[abc]a、 b或c简单类
[^abc]任何字符除了 a、 b或c否定
[a-zA-Z]a到z或A到Z两头的字母包括在内范围
[a-z&&[^bc]]a到z 除了 b和c [ad-z] 减去
[a-z&&[^m-p]]a到z 而非m到p [a-lq-z] 减去
数量词
X?X一次或一次也没有
X*X零次或多次
X+X一次或多次
X{n}X恰好n次
X{n,}X至少n次
X{n,m}X至少n次但是不超过m次
System out println("?一次或者一次都没有"+"1"matches("\\d?"));
System out println("*零次或多次"+"1"matches("\\d*"));
System out println("+一次或多次"+"1"matches("\\d+"));
System out println("{n}恰好n次"+"1"matches("\\d{1}"));
System out println("{n,}至少n次"+"1"matches("\\d{1,}"));
System out println("{n,m}至少n次但是不超过m次"+"1"matches("\\d{1,2}"));?一次或者一次都没有t ru e
*零次或多次t ru e
+恰好n次true
{n}至少n次true
{n,}至少n次true
{n,m}至少n次但是不超过m次t ru eimportjava.util.HashSet;importjava.util.Iterator;importjava.util.Scanner;importjava.util.Set;classUser{
String Account;
String PassWord;
publicUser(String Account,StringPassWord) {this.Account = Account;this.PassWord = PassWord;
}
@Overridepublic String toString() {return"{"+this.Account+" "+this.PassWord+"}";}
}publicclass Main {static Scanner scanner = new Scanner(System.in);static Set<User>users = newHashSet<User>();publicstaticvoid main(String[ ] args) {while(true) {
Meau();
String chioce = scanner.next();if("a" .equalsIgnoreCase(chioce) ) {
System.out.println("请输入账号 ");
String Account = scanner.next();
System.out.println("请输入密码 ");
String Password = scanner.next();
Iteratorit = users.iterator() ;booleanisLogin = false;while(it.hasNext() ) {
User user = (User) it.next();if(Account.equals(user.Account)&&Password.equals(user.PassWord)) {isLogin = true;break;
}
}if(isLogin) {
System.out.println("登录成功");try {
Thread.sleep(1000);
} catch (InterruptedExceptione) {// TODO Auto-generated catch blocke.printStackTrace();
}
}else {
System.out.println("登录失败");
}
}elseif("b" .equalsIgnoreCase(chioce) ) {
System.out.println("请输入账号 ");
String Account = scanner.next();
System.out.println("请输入密码 ");
String Password = scanner.next();if(Account.matches(" [a-zA-Z]\\d{6}\\w{0,3}")&&Password.matches("\\D\\d{2,}@") ) {
System.out.println("注册成功");users.add(new User(Account, Password));
}else{
System.out.println("注册失败");
}
}elseif("show" .equals(chioce) ) {
System.out.println("当前用户有 "+users);
}else {
System.out.println("无效选项请重新输入! ");
}
}
}publicstaticvoidMeau() {
System.out.println("*************");
System.out.println("*A登录 B注册*") ;
System.out.println("*************");
}
}
//账号必须以字母开头+六位数字+最多不超过10个字
//密码要求以非字母开头+至少两位数字+以@结尾
if(Account.matches(" [a-zA-Z]\\d{6}\\w{0,3}")
&&Password.matches("\\D\\d{2,}@") )classUser{
String Account;
String PassWord;publicUser(String Account,StringPassWord) {this.Account = Account;this.PassWord = PassWord;
}
@Overridepublic String toString() {return"{"+this.Account+" "+this.PassWord+"}";}
}
因为使用了Set集合static Set<User>users = newHashSet<User>()users.add(new User(Account, Password));
在User中要重写toString方法才能将User类中的数据给输出
Hostodo又发布了几款针对7月4日美国独立日的优惠套餐(Independence Day Super Sale),均为年付,基于KVM架构,采用NVMe硬盘,最低13.99美元起,可选拉斯维加斯或者迈阿密机房。这是一家成立于2014年的国外VPS主机商,主打低价VPS套餐且年付为主,基于OpenVZ和KVM架构,产品性能一般,支持使用PayPal或者支付宝等付款方式。商家客服响应也比较一般,推...
virmach怎么样?virmach家这几年非常火,从商家的黑五闪购开始,以超低的价格吸引了大批的国人客户,而且商家的机器还是非常稳定的,站长手里的4.75刀年付已经用了两年了,非常稳定,不过商家到国内的线路一般,目前商家新上了夏季优惠促销,价格低到发指,年付7.2美元起,商家反馈将在9月开始更换AMD+NVMe平台,这个消息从年初就有了,不过一直没有更换,目前这个时间也不确定是否准确。点击进入:...
百纵科技:美国高防服务器,洛杉矶C3机房 独家接入zenlayer清洗 带金盾硬防,CPU全系列E52670、E52680v3 DDR4内存 三星固态盘阵列!带宽接入了cn2/bgp线路,速度快,无需备案,非常适合国内外用户群体的外贸、搭建网站等用途。官方网站:https://www.baizon.cnC3机房,双程CN2线路,默认200G高防,3+1(高防IP),不限流量,季付送带宽美国洛杉矶C...