正则表达式是对字符串操作的一种逻辑公式就是用事先定义好的一些特定字符、及这些特定字符的组合组成一个“规则字符串”这个“规则字符串”用来表达对字符串的一种过滤逻辑
预定义字符类
.任意字符
\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类中的数据给输出
licloud怎么样?licloud目前提供香港cmi服务器及香港CN2+BGP服务器/E3-1230v2/16GB内存/240GB SSD硬盘/不限流量/30Mbps带宽,$39.99/月。licloud 成立於2021年,是香港LiCloud Limited(CR No.3013909)旗下的品牌,主要提供香港kvm vps,分为精简网络和高级网络A、高级网络B,现在精简网络和高级网络A。现在...
LOCVPS(全球云)发布了新上韩国机房KVM架构主机信息,提供流量和带宽方式,适用全场8折优惠码,优惠码最低2G内存套餐月付仅44元起。这是一家成立较早的国人VPS服务商,目前提供洛杉矶MC、洛杉矶C3、和香港邦联、香港沙田电信、香港大埔、日本东京、日本大阪、新加坡、德国和荷兰等机房VPS主机,基于KVM或者XEN架构。下面分别列出几款韩国机房KVM主机配置信息。韩国KVM流量型套餐:KR-Pl...
云基成立于2020年,目前主要提供高防海内外独立服务器用户,欢迎各类追求稳定和高防优质线路的用户。业务可选:洛杉矶CN2-GIA+高防(默认500G高防)、洛杉矶CN2-GIA(默认带50Gbps防御)、香港CN2-GIA高防(双向CN2GIA专线,突发带宽支持,15G-20G DDoS防御,无视CC)、国内高防服务器(广州移动、北京多线、石家庄BGP、保定联通、扬州BGP、厦门BGP、厦门电信、...