为包含字母或数字的字符串快速创建正则表达式

问题描述 投票:0回答:1

我想创建正则表达式,我可以检查字符串是否包含任何大写或小写字母,并且还包含应该返回 true 的数字。

如果我可以限制字符数示例:仅限 64 个字符

示例:

Apple = True
aPPle = True
App1e = True
@apple = true
!^%$# = false (because it does not contains any letters or digits.)

我尝试过的正则表达式如下,但它不起作用:

let localRegex = "[A-Za-z0-9]{1,64}\\."
swift regex nsregularexpression
1个回答
0
投票
let input = "Your_Input_String_Here"
let regex = "^(?=.*[a-zA-Z])(?=.*\\d).*$"

if let range = input.range(of: regex, options: .regularExpression) {
    print("True")
} else {
    print("False")
}
© www.soinside.com 2019 - 2024. All rights reserved.