java-用于验证用户名的正则表达式
我正在尝试创建一个正则表达式来根据以下条件验证用户名:
仅包含字母数字字符,下划线和点。
下划线和点不能在结尾或用户名的开头(例如user__name/user..name/.username/username.)。
下划线和点不能彼此相邻(例如user__name)。
下划线或点不能连续使用多次(例如user__name/user..name)。
字符数必须在8到20之间。
到目前为止,这是我所做的; 听起来它强制执行所有准则规则,但第5条规则除外。 我不知道如何添加第五条规则:
^[a-zA-Z0-9]+([._]?[a-zA-Z0-9]+)*$
8个解决方案
181 votes
^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?
└─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
│ │ │ │ no _ or . at the end
│ │ │ │
│ │ │ allowed characters
│ │ │
│ │ no __ or _. or ._ or .. inside
│ │
│ no _ or . at the beginning
│
username is 8-20 characters long
Ωmega answered 2020-08-06T08:25:30Z
8 votes
对Phillip的答案稍作修改即可解决最新要求
^[a-zA-Z0-9]([._](?![._])|[a-zA-Z0-9]){6,18}[a-zA-Z0-9]$
Patrick McDonald answered 2020-08-06T08:25:50Z
7 votes
我想您必须在这里使用Lookahead表达式。 [http://www.regular-expressions.info/lookaround.html]
尝试
[a-zA-Z0-9]
[a-zA-Z0-9]一个字母数字的THEN(
[a-zA-Z0-9] a _后面没有。 要么
[a-zA-Z0-9]一个。 不跟_ OR
[a-zA-Z0-9]字母数字)FOR
[a-zA-Z0-9]最小6到最大18倍THEN
[a-zA-Z0-9]一个字母数字
(第一个字符为字母数字,然后为6至18个字符,最后一个字符为字母数字,6 + 2 = 8,18 + 2 = 20)
PhilMasterG answered 2020-08-06T08:26:45Z
2 votes
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
String userName = scan.nextLine();
String regularExpression = "^[[A-Z]|[a-z]][[A-Z]|[a-z]|\\d|[_]]{7,29}$";
if (userName.matches(regularExpression)) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
}
}
isurie answered 2020-08-06T08:27:00Z
1 votes
我喜欢正则表达式,但我认为可读性是有限的
所以我建议
new Regex("^[a-z._]+$", RegexOptions.IgnoreCase).IsMatch(username) &&
!username.StartsWith(".") &&
!username.StartsWith("_") &&
!username.EndsWith(".") &&
!username.EndsWith("_") &&
!username.Contains("..") &&
!username.Contains("__") &&
!username.Contains("._") &&
!username.Contains("_.");
它更长,但是不需要维护人员打开expresso即可理解。
当然,您可以评论一个长的正则表达式,但是读过它的人必须依靠信任...。
James Kyburz answered 2020-08-06T08:27:33Z
1 votes
^ [a-z0-9 _-] {3,15} $
^#行首
[a-z0-9_-]#匹配列表中的字符和符号,a-z,0-9,下划线,连字符
{3,15}#长度至少3个字符,最大长度为15
$#行尾
Mehul Jariwala answered 2020-08-06T08:28:11Z
0 votes
这应该可以解决问题:
if (Regex.IsMatch(text, @"
# Validate username with 5 constraints.
^ # Anchor to start of string.
# 1- only contains alphanumeric characters , underscore and dot.
# 2- underscore and dot can't be at the end or start of username,
# 3- underscore and dot can't come next to each other.
# 4- each time just one occurrence of underscore or dot is valid.
(?=[A-Za-z0-9]+(?:[_.][A-Za-z0-9]+)*$)
# 5- number of characters must be between 8 to 20.
[A-Za-z0-9_.]{8,20} # Apply constraint 5.
$ # Anchor to end of string.
", RegexOptions.IgnorePatternWhitespace))
{
// Successful match
} else {
// Match attempt failed
}
ridgerunner answered 2020-08-06T08:28:31Z
0 votes
很抱歉,我是从我自己的库中生成的,并且它使用了对Dart / Javascript / Java / Python有效的语法,但是无论如何,这里是这样:
(?:^)(?:(?:[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKlMNOPQRSTUVWXYZ0123456789]){1,1})(?!(?:(?:(?:(?:_\.){1,1}))|(?:(?:(?:__){1,1}))|(?:(?:(?:\.\.){1,1}))))(?:(?:(?:(?:[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKlMNOPQRSTUVWXYZ0123456789._]){1,1})(?!(?:(?:(?:(?:_\.){1,1}))|(?:(?:(?:__){1,1}))|(?:(?:(?:\.\.){1,1}))))){6,18})(?:(?:[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKlMNOPQRSTUVWXYZ0123456789]){1,1})(?:$)
我的图书馆代码:
var alphaNumeric = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "l", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var allValidCharacters = new List.from(alphaNumeric);
allValidCharacters.addAll([".", "_"]);
var invalidSequence = (r) => r
.eitherString("_.")
.orString("__")
.orString("..");
var regex = new RegExpBuilder()
.start()
.exactly(1).from(alphaNumeric).notBehind(invalidSequence)
.min(6).max(18).like((r) => r.exactly(1).from(allValidCharacters).notBehind(invalidSequence))
.exactly(1).from(alphaNumeric)
.end()
.getRegExp();
我的图书馆:[https://github.com/thebinarysearchtree/RegExpBuilder]
answered 2020-08-06T08:29:00Z