问个正则

theoffspring 2012-04-21 10:02:38
要求匹配的字符串必须至少有一个英文字母,一个数字,“@#*=”中的一个,测试文本如下:
1234@a
ABC3a#@
1Ac@
ABC 3a#@

只有第二个是匹配的,正则怎么写?
我试写了一下,([a-zA-Z]|\d|[@#\*=])+,这个是不行的,因为第一个也匹配上了。
...全文
217 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
theoffspring 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

String str="ABC3a#@";
char[] characters = str.trim().toCharArray();
int[] flags = new int[];
for(char c:characters){
if(String.valueOf(c).matches("[\\s]")){
System.out.print("不匹配");
return;
}
……
[/Quote]

这个办法好。就是效率有点低。题目还是漏说了一点要求,字符长度要求在5-10之间,所以第三个也不匹配,贴出完整的题目吧。

1. The minimum length of the username must be 5 characters and the maximum may be 10.
2. It should contain at least one letter from A-Z
3. It should contain at least one digit from 0-9
4. It should contain at least one character from amongst @#*=
5. It should not contain any spaces

Write a program which accepts 4 usernames (one username per line) as input and checks whether each of them satisfy the above mentioned conditions.
If a username satisfies the conditions, the program should print PASS (in uppercase)
If a username fails the conditions, the program should print FAIL (in uppercase)

Suppose the following usernames are supplied to the program:
1234@a
ABC3a#@
1Ac@
ABC 3a#@

Then the output should be:
FAIL
PASS
FAIL
FAIL
wu209000 2012-04-24
  • 打赏
  • 举报
回复
.*[a-zA-Z]|\d|[@#\*=].*
zhengshaowu 2012-04-24
  • 打赏
  • 举报
回复
我写的是:匹配的字符串必须至少有一个英文字母,一个数字,“@#*=”中的一个.但不是职匹配第二个的
zhengshaowu 2012-04-24
  • 打赏
  • 举报
回复
(.*\d+.*[a-zA-Z]+.*[@#*=]+.*)|(.*\d+.*[@#*=]+.*[a-zA-Z]+.*)|(.*[a-zA-Z]+.*[@#*=]+.*\d+.*)|(.*[@#*=]+.*[a-zA-Z]+.*\d+.*)|(.*[a-zA-Z]+.*\d+.*[@#*=]+.*)|(.*[@#*=]+.*\d+.*[a-zA-Z]+.*)
OPPPPOP 2012-04-23
  • 打赏
  • 举报
回复
用3次检查 简单实用
安特矮油 2012-04-23
  • 打赏
  • 举报
回复
你就去find吧,如果没有找到a-z,A-Z,\\d,[@#*=],就不满足,如果找到了\\s+那么也不满足
「已注销」 2012-04-23
  • 打赏
  • 举报
回复
regex = "[a-zA-Z]+\\d+[a-zA-Z]?[@#*=]+"
「已注销」 2012-04-23
  • 打赏
  • 举报
回复
regex = "[a-zA-Z]+\\d+[a-zA-Z]?[@#*=]+"
chen5287603 2012-04-23
  • 打赏
  • 举报
回复
"\\p{Upper}+\\d+\\p{Lower}+[@#\\*=]+";
这个正则是在有顺序的情况下才能匹配成功。因为正则引擎在解析时也是按照从左到右的顺来匹配的。如果你不怕麻烦的话,可以把这四种字符的所有排列组合都写出来,然后用或来匹配即“A|B|C|D”A、B、C、D分别代表一种组合
shine333 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:

引用 2 楼 的回复:

至少有一个英文字母,一个数字,“@#*=”:意思是至少有一个英文字母,至少有一个数字,至少 有“@#*=”中的一个。明白了吧。

1234@a 也有字母,有数字,有@什么的

不好意思,漏写了一点,大小写字母都要有。
[/Quote]
那为什么第四个不匹配?不能有这4类以外的东西?
1 大写
2 小写
3 数字
4 @#*=
必须有且只能有这4类??
sdojqy1122 2012-04-22
  • 打赏
  • 举报
回复
String str="ABC3a#@";
char[] characters = str.trim().toCharArray();
int[] flags = new int[];
for(char c:characters){
if(String.valueOf(c).matches("[\\s]")){
System.out.print("不匹配");
return;
}
if(String.valueOf(c).matches("[A-Z]")){
flags[0]=1;
}
if(String.valueOf(c).matches("[@#\\*=]")){
flags[1]=1;
}
if(String.valueOf(c).matches("[a-z]")){
flags[2]=1;
}
if(String.valueOf(c).matches("[0-9]")){
flags[3]=1;
}
}
if(flags[0]==1&&flags[1]==1&&flags[2]==1&&flags[3]==1){
System.out.print("匹配");
}
theoffspring 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

这个要写正则有点难,即使写出来了,可读性也不高。建议楼主用常规方法。
[/Quote]
常规方法是什么?
theoffspring 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 5 楼 的回复:

引用 4 楼 的回复:

引用 2 楼 的回复:

至少有一个英文字母,一个数字,“@#*=”:意思是至少有一个英文字母,至少有一个数字,至少 有“@#*=”中的一个。明白了吧。

1234@a 也有字母,有数字,有@什么的

不好意思,漏写了一点,大小写字母都要有。

那为什么第四个不匹配?不能有这4类以外的东西?
1 大写
2 小写……
[/Quote]
不好意思,又漏了一点,不能有空格,这下补全了。
sdojqy1122 2012-04-21
  • 打赏
  • 举报
回复
这个要写正则有点难,即使写出来了,可读性也不高。建议楼主用常规方法。
theoffspring 2012-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 2 楼 的回复:

至少有一个英文字母,一个数字,“@#*=”:意思是至少有一个英文字母,至少有一个数字,至少 有“@#*=”中的一个。明白了吧。

1234@a 也有字母,有数字,有@什么的
[/Quote]
不好意思,漏写了一点,大小写字母都要有。
WayneXuan 2012-04-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

至少有一个英文字母,一个数字,“@#*=”:意思是至少有一个英文字母,至少有一个数字,至少 有“@#*=”中的一个。明白了吧。
[/Quote]
1234@a 也有字母,有数字,有@什么的
theoffspring 2012-04-21
  • 打赏
  • 举报
回复
奇怪,回复看不到
theoffspring 2012-04-21
  • 打赏
  • 举报
回复
至少有一个英文字母,一个数字,“@#*=”:意思是至少有一个英文字母,至少有一个数字,至少 有“@#*=”中的一个。明白了吧。
WayneXuan 2012-04-21
  • 打赏
  • 举报
回复
具体要求是什么?第一个也满足"至少有一个英文字母,一个数字,“@#*=”中的一个"

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧