字符正则表达式JAVA版本。

java正则表达式  时间:2021-01-31  阅读:()

正则表达式是对字符串操作的一种逻辑公式就是用事先定义好的一些特定字符、及这些特定字符的组合组成一个“规则字符串”这个“规则字符串”用来表达对字符串的一种过滤逻辑

预定义字符类

.任意字符

\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类中的数据给输出

青云互联:美国洛杉矶CN2弹性云限时八折,15元/月起,可选Windows/可自定义配置

青云互联怎么样?青云互联是一家成立于2020年6月的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、香港服务器、美国云服务器,让您的网站高速、稳定运行。美国cn2弹性云主机限时8折起,可选1-20个IP,仅15元/月起,附8折优惠码使用!点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联活动方案:美国洛杉矶...

RAKsmart推出7.59美元/月,云服务器产品Cloud Server,KVM架构1核1G内存40G硬盘1M带宽基础配置

近期RAKsmart上线云服务器Cloud Server产品,KVM架构1核1G内存40G硬盘1M带宽基础配置7.59美元/月!RAKsmart云服务器Cloud Server位于美国硅谷机房,下单可选DIY各项配置,VPC网络/经典网络,大陆优化/精品网线路,1-1000Mbps带宽,支持Linux或者Windows操作系统,提供Snap和Backup。RAKsmart机房是一家成立于2012年...

bluehost32元/月,2核2G/20GB空间,独立ip,新一代VPS美国云主机!

bluehost怎么样?bluehost推出新一代VPS美国云主机!前几天,BlueHost也推出了对应的周年庆活动,全场海外虚拟主机月付2.95美元起,年付送免费的域名和SSL证书,通过活动进入BlueHost中文官网,购买虚拟主机、云虚拟主机和独立服务器参与限时促销。今天,云服务器网(yuntue.com)小编给大家介绍的是新一代VPS美国云主机,美国SSD云主机,2核2G/20GB空间,独立...

java正则表达式为你推荐
手动挡和自动挡哪个好手动挡和自动挡哪个好少儿英语哪个好少儿英语,那个好一些?牡丹江教育云空间登录云端学习如何登录?东莞电信宽带套餐东莞光纤宽带资费360云u盘360云U盘自动备份有什么用360云盘转存百度云盘如何把360云盘文件转存到百度云网盘电影票在哪买便宜网上哪里买电影票便宜10个比1688便宜的拿货网推荐几个淘宝网上买东西便宜,东西又好的网站。。。便宜英文便宜的所有英语单词最便宜的敞篷车敞篷车大概多少钱一辆便宜点的。
重庆虚拟主机 vps服务器 动态域名解析软件 韩国俄罗斯 Vultr mach5 cpanel 42u机柜尺寸 免费ftp空间申请 韩国网名大全 华为网络硬盘 双拼域名 bgp双线 域名接入 免费网页空间 服务器是干什么用的 宏讯 什么是web服务器 七牛云存储 apachetomcat 更多