匹配 UID 的正则表达式
Regular expression to match an UID
我正在尝试验证具有以下规则的 UID
- 它必须包含至少 2 个大写英文字母字符。
- 它必须包含至少 3 个数字 (0-9)。
- 它应该只包含字母数字字符。
- 任何字符都不应重复。
- 有效的 UID 中必须恰好有 10 个字符。
我试过用正则表达式来做,但我无法创建一个可以满足所有规则的模式。
所以我没有使用正则表达式。
#import re
#pattern = r'^[A-Z0-9]{10}$'
testCases = int(input())
for _ in range(testCases):
uid = input()
if len(uid) == 10 and len(uid) == len(set(uid)):
countChr = countNum = 0
for i in uid:
if i.isdigit():
countNum += 1
elif i.isalpha():
countChr +=1
if countChr >= 2 and countNum >= 3:
print("Valid")
else:
print("Invalid")
else:
print("Invalid")
上面的程序完美运行,但我想用正则表达式验证 UID,那么有什么模式可以满足所有给定的规则吗?
let regex = /^(?!.*?(.).*?)(?=(?:.*?[A-Z]){2,})(?=(?:.*?\d){3,})[a-zA-Z\d]{10}$/;
console.log(regex.test("A0ieu5Wsl2"));
console.log(regex.test("A0ieuiWsl2"));
console.log(regex.test("A0ieu5Wsl2l"));
试试这个正则表达式:
^(?=(?:.*[A-Z]){2})(?=(?:.*[0-9]){3})(?:([a-zA-Z0-9])(?!.*)){10}$
^ beginning of the text
(?= positive lookahead
(?: non-capturing group
.*[A-Z] anything followed by an uppercased letter
){2} match 2 of them
) end of the lookahead
(?= positive lookahead
(?: non-capturing group
.*[0-9] anything followed by a digit
){3} match 3 of them
) end of the lookahead
(?: non-capturing group
([a-zA-Z0-9]) any alphanumeric character and put it in group 1
(?!.*) negative lookahead, make sure there's no repeated character ahead
){10} match 10 of them
$ end of the text
使用python
import re
pattern = r'^(?=(?:.*[A-Z]){2})(?=(?:.*[0-9]){3})(?:([a-zA-Z0-9])(?!.*)){10}$'
testCases = int(input())
for _ in range(testCases):
uid = input()
if re.match(pattern, uid):
print("Valid")
else:
print("Invalid")
我正在尝试验证具有以下规则的 UID
- 它必须包含至少 2 个大写英文字母字符。
- 它必须包含至少 3 个数字 (0-9)。
- 它应该只包含字母数字字符。
- 任何字符都不应重复。
- 有效的 UID 中必须恰好有 10 个字符。
我试过用正则表达式来做,但我无法创建一个可以满足所有规则的模式。
所以我没有使用正则表达式。
#import re
#pattern = r'^[A-Z0-9]{10}$'
testCases = int(input())
for _ in range(testCases):
uid = input()
if len(uid) == 10 and len(uid) == len(set(uid)):
countChr = countNum = 0
for i in uid:
if i.isdigit():
countNum += 1
elif i.isalpha():
countChr +=1
if countChr >= 2 and countNum >= 3:
print("Valid")
else:
print("Invalid")
else:
print("Invalid")
上面的程序完美运行,但我想用正则表达式验证 UID,那么有什么模式可以满足所有给定的规则吗?
let regex = /^(?!.*?(.).*?)(?=(?:.*?[A-Z]){2,})(?=(?:.*?\d){3,})[a-zA-Z\d]{10}$/;
console.log(regex.test("A0ieu5Wsl2"));
console.log(regex.test("A0ieuiWsl2"));
console.log(regex.test("A0ieu5Wsl2l"));
试试这个正则表达式:
^(?=(?:.*[A-Z]){2})(?=(?:.*[0-9]){3})(?:([a-zA-Z0-9])(?!.*)){10}$
^ beginning of the text
(?= positive lookahead
(?: non-capturing group
.*[A-Z] anything followed by an uppercased letter
){2} match 2 of them
) end of the lookahead
(?= positive lookahead
(?: non-capturing group
.*[0-9] anything followed by a digit
){3} match 3 of them
) end of the lookahead
(?: non-capturing group
([a-zA-Z0-9]) any alphanumeric character and put it in group 1
(?!.*) negative lookahead, make sure there's no repeated character ahead
){10} match 10 of them
$ end of the text
使用python
import re
pattern = r'^(?=(?:.*[A-Z]){2})(?=(?:.*[0-9]){3})(?:([a-zA-Z0-9])(?!.*)){10}$'
testCases = int(input())
for _ in range(testCases):
uid = input()
if re.match(pattern, uid):
print("Valid")
else:
print("Invalid")