简体   繁体   English

Java Regex - 字母 + 数字最少为 3

[英]Java Regex - Alphabet + numbers with minimum total of 3

What have I been doing so far?到目前为止我一直在做什么?

I have been searching on internet for a full hour now, but I just can't seem to find it, most answers are based on just alphabetic characters or only digits.我已经在互联网上搜索了整整一个小时,但我似乎无法找到它,大多数答案仅基于字母字符或仅基于数字。 I'm a beginner with regex and I'm trying to learn it but I just don't get it why this wouldn't work.我是正则表达式的初学者,我正在尝试学习它,但我不明白为什么这不起作用。

The problem问题

What I want is that (for a search machine) the input is validated with a pattern.我想要的是(对于搜索机)输入是用模式验证的。 This pattern must consist out of a minimum combination of 3 letters and or numbers.此模式必须由至少 3 个字母和/或数字的组合组成。 Special characters are allowed as well but there must be at least a total of 3 letters or numbers in the input.也允许使用特殊字符,但输入中必须至少包含 3 个字母或数字。

What have I tried?我尝试了什么?

I tried using (([aA-zZ]{0,}[0-9]{0,}){3,})\\w+ But this doesn't allow special characters nor does it get that I want 3 subsequential characters, because 2 are also allowed我尝试使用(([aA-zZ]{0,}[0-9]{0,}){3,})\\w+但这不允许特殊字符,也没有得到我想要 3 个后续字符,因为 2 也是允许的

So what would be correct and what would be incorrect?那么什么是正确的,什么是不正确的呢?

asdA - correct - more than three in a row asdA -正确- 连续超过三个

as - incorrect - less than three in a row作为 -不正确- 连续少于三个

a1s - correct - three in a row a1s -正确- 连续三个

+a1s/ correct - three in a row +a1s/正确- 连续三个

+a1-s - incorrect - less than 3 in a row +a1-s -不正确- 连续少于 3 个

If you need to validate 3 consecutive letter or numbers then use this regex:如果您需要验证 3 个连续的字母或数字,请使用此正则表达式:

[A-Za-z0-9]{3}

In java use:在java中使用:

str.matches(".*[A-Za-z0-9]{3}.*");

Try using something like:尝试使用类似的东西:

 System.out.println("asdA".matches(".*?[a-zA-Z0-9]{3}.*?"));
 System.out.println("as".matches(".*?[a-zA-Z0-9]{3}.*?"));
 System.out.println("a1s".matches(".*?[a-zA-Z0-9]{3}.*?"));
 System.out.println("+a1s/".matches(".*?[a-zA-Z0-9]{3}.*?"));
 System.out.println("+a1-s".matches(".*?[a-zA-Z0-9]{3}.*?"));

我认为这对你有用:

\w*([a-zA-Z0-9]){3,}\w*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM