正则表达式是对字符串操作的一种逻辑公式就是用事先定义好的一些特定字符、及这些特定字符的组合组成一个“规则字符串”这个“规则字符串”用来表达对字符串的一种过滤逻辑
预定义字符类
.任意字符
\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类中的数据给输出
昨天有在"盘点2021年主流云服务器商家618年中大促活动"文章中整理到当前年中大促618活动期间的一些国内国外的云服务商的促销活动,相对来说每年年中和年末的活动力度还是蛮大的,唯独就是活动太过于密集,而且商家比较多,导致我们很多新人不懂如何选择,当然对于我们这些老油条还是会选择的,估计没有比我们更聪明的进行薅爆款新人活动。有网友提到,是否可以整理一篇当前的这些活动商家中的促销产品。哪些商家哪款产...
ZJI是成立于2011年原Wordpress圈知名主机商—维翔主机,2018年9月更名为ZJI,主要提供香港、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册业务。本月商家针对香港阿里云线路独立服务器提供月付立减270-400元优惠码,优惠后香港独立服务器(阿里云专线)E3或者E5 CPU,SSD硬盘,最低每月仅480元起。阿里一型CPU:Intel E5-2630L...
提速啦的来历提速啦是 网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑 由赣州王成璟网络科技有限公司旗下赣州提速啦网络科技有限公司运营 投资1000万人民币 在美国Cera 香港CTG 香港Cera 国内 杭州 宿迁 浙江 赣州 南昌 大连 辽宁 扬州 等地区建立数据中心 正规持有IDC ISP CDN 云牌照 公司。公司购买产品支持3天内退款 超过3天步退款政策。提速啦的市场定位提速啦主...