正则表达式是对字符串操作的一种逻辑公式就是用事先定义好的一些特定字符、及这些特定字符的组合组成一个“规则字符串”这个“规则字符串”用来表达对字符串的一种过滤逻辑
预定义字符类
.任意字符
\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类中的数据给输出
ttcloud怎么样?ttcloud是一家海外服务器厂商,运营服务器已经有10年时间,公司注册地址在香港地区,业务范围包括服务器托管,机柜托管,独立服务器等在内的多种服务。我们后台工单支持英文和中文服务。TTcloud最近推出了新上架的日本独立服务器促销活动,价格 $70/月起,季付送10Mbps带宽。也可以跟进客户的需求进行各种DIY定制。点击进入:ttcloud官方网站地址TTcloud拥有自...
提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...
官方网站:点击访问CDN客服QQ:123008公司名:贵州青辞赋文化传媒有限公司域名和IP被墙封了怎么办?用cloudsecre.com网站被攻击了怎么办?用cloudsecre.com问:黑客为什么要找网站来攻击?答:黑客需要找肉鸡。问:什么是肉鸡?答:被控的服务器和电脑主机就是肉鸡。问:肉鸡有什么作用?答:肉鸡的作用非常多,可以用来干违法的事情,通常的行为有:VPN拨号,流量P2P,攻击傀儡,...