I am trying to create a method that finds whether a string contains a number , Upper case letter and a special character using regular expression as below
我正在尝试创建一个方法,使用正则表达式查找字符串是否包含数字、大写字母和特殊字符,如下所示
func checkTextSufficientComplexity(var text : String) -> Bool{
let capitalLetterRegEx = "[A-Z]+"
var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
var capitalresult = texttest.evaluateWithObject("AniP")
println("\(capitalresult)")
let numberRegEx = "[0-9]+"
var texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
var numberresult = texttest1.evaluateWithObject(text)
println("\(numberresult)")
let specialCharacterRegEx = "[.*&^%$#@()/]+"
var texttest2 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
var specialresult = texttest2.evaluateWithObject(text)
println("\(specialresult)")
return capitalresult && numberresult && specialresult
}
The problem is the below regular expression [A-Z]+ returns true for only e.g AVATAR and returns false for Avatar. I want my regular expression return true if it contains at least one UpperCase in String.
问题是下面的正则表达式[A-Z]+仅对e返回true。《阿凡达》和《阿凡达》都是假的。如果正则表达式在字符串中包含至少一个大写字母,我希望它返回true。
3 个解决方案
#1
23
Simply replace your RegEx rule [A-Z]+ with .*[A-Z]+.* (and other RegEx rules as well)
只需将RegEx规则[A-Z]+替换为.*[A-Z]+。*(以及其他RegEx规则)
Rules
规则
[A-Z]+ matches only strings with all characters capitalized
+只匹配所有字符大写的字符串
Examples: AVATAR, AVA, TAR, AAAAAA
Won't work: AVATAr
阿凡达,艾娃,焦油,AAAAAA都不行:阿凡达
.* matches all strings (0+ characters)
.*匹配所有字符串(0+字符)
Examples: 1, 2, AVATAR, AVA, TAR, a, b, c
1、2、阿凡达、AVA、TAR、a、b、c
.*[A-Z]+.* matches all strings with at least one capital letter
,*[a - z]+。*将所有字符串匹配为至少一个大写字母
Examples: Avatar, avataR, aVatar
例子:《阿凡达》,《阿凡达》,《阿凡达》
Explanation:
解释:
I. .* will try to match 0 or more of anything
II. [A-Z]+ will require at least one capital letter (because of the +)
III. .* will try to match 0 or more of anything
I. *将尝试匹配0或更多的任何II。[A-Z]+将需要至少一个大写字母(因为+).*将尝试匹配0或更多的任何东西
Avatar [empty | "A" | "vatar"]
aVatar ["a" | "V" | "atar"]
aVAtar ["a" | "VA" | "tar"]《阿凡达》(空|“A”|“vatar”)《阿凡达》(“A”|“V”|“阿塔尔”)《阿凡达》(“A”|“VA”|“焦油”)
Working Code
工作代码
func checkTextSufficientComplexity(var text : String) -> Bool{
let capitalLetterRegEx = ".*[A-Z]+.*"
var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
var capitalresult = texttest!.evaluateWithObject(text)
println("\(capitalresult)")
let numberRegEx = ".*[0-9]+.*"
var texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
var numberresult = texttest1!.evaluateWithObject(text)
println("\(numberresult)")
let specialCharacterRegEx = ".*[!&^%$#@()/]+.*"
var texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)
var specialresult = texttest2!.evaluateWithObject(text)
println("\(specialresult)")
return capitalresult || numberresult || specialresult
}
Examples:
例子:
checkTextSufficientComplexity("Avatar") // true || false || false
checkTextSufficientComplexity("avatar") // false || false || false
checkTextSufficientComplexity("avatar1") // false || true || false
checkTextSufficientComplexity("avatar!") // false || false || true
#2
7
Here is a concise version of Joshuas answer in Swift 3, assuming that all validations must be fulfilled.
这里有一个简短版本的Joshuas回答在Swift 3,假设所有的验证必须完成。
func validate(password: String) -> Bool {
let capitalLetterRegEx = ".*[A-Z]+.*"
let texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
guard texttest.evaluate(with: password) else { return false }
let numberRegEx = ".*[0-9]+.*"
let texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
guard texttest1.evaluate(with: password) else { return false }
let specialCharacterRegEx = ".*[!&^%$#@()/_*+-]+.*"
let texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)
guard texttest2.evaluate(with: password) else { return false }
return true
}
#3
3
Another alternate solution.
另一个备选的解决方案。
You can do all the check using one regular expression.
您可以使用一个正则表达式来完成所有的检查。
RegExp: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}
You can use this as mention below:
你可以使用如下所述:
//Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
let regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}"
let isMatched = NSPredicate(format:"SELF MATCHES %@", regex).evaluate(with: yourTextField.text)
if(isMatched == true) {
// Do your stuff ..
} else {
// Show Error Message.
}
#1
23
Simply replace your RegEx rule [A-Z]+ with .*[A-Z]+.* (and other RegEx rules as well)
只需将RegEx规则[A-Z]+替换为.*[A-Z]+。*(以及其他RegEx规则)
Rules
规则
[A-Z]+ matches only strings with all characters capitalized
+只匹配所有字符大写的字符串
Examples: AVATAR, AVA, TAR, AAAAAA
Won't work: AVATAr
阿凡达,艾娃,焦油,AAAAAA都不行:阿凡达
.* matches all strings (0+ characters)
.*匹配所有字符串(0+字符)
Examples: 1, 2, AVATAR, AVA, TAR, a, b, c
1、2、阿凡达、AVA、TAR、a、b、c
.*[A-Z]+.* matches all strings with at least one capital letter
,*[a - z]+。*将所有字符串匹配为至少一个大写字母
Examples: Avatar, avataR, aVatar
例子:《阿凡达》,《阿凡达》,《阿凡达》
Explanation:
解释:
I. .* will try to match 0 or more of anything
II. [A-Z]+ will require at least one capital letter (because of the +)
III. .* will try to match 0 or more of anything
I. *将尝试匹配0或更多的任何II。[A-Z]+将需要至少一个大写字母(因为+).*将尝试匹配0或更多的任何东西
Avatar [empty | "A" | "vatar"]
aVatar ["a" | "V" | "atar"]
aVAtar ["a" | "VA" | "tar"]《阿凡达》(空|“A”|“vatar”)《阿凡达》(“A”|“V”|“阿塔尔”)《阿凡达》(“A”|“VA”|“焦油”)
Working Code
工作代码
func checkTextSufficientComplexity(var text : String) -> Bool{
let capitalLetterRegEx = ".*[A-Z]+.*"
var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
var capitalresult = texttest!.evaluateWithObject(text)
println("\(capitalresult)")
let numberRegEx = ".*[0-9]+.*"
var texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
var numberresult = texttest1!.evaluateWithObject(text)
println("\(numberresult)")
let specialCharacterRegEx = ".*[!&^%$#@()/]+.*"
var texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)
var specialresult = texttest2!.evaluateWithObject(text)
println("\(specialresult)")
return capitalresult || numberresult || specialresult
}
Examples:
例子:
checkTextSufficientComplexity("Avatar") // true || false || false
checkTextSufficientComplexity("avatar") // false || false || false
checkTextSufficientComplexity("avatar1") // false || true || false
checkTextSufficientComplexity("avatar!") // false || false || true
#2
7
Here is a concise version of Joshuas answer in Swift 3, assuming that all validations must be fulfilled.
这里有一个简短版本的Joshuas回答在Swift 3,假设所有的验证必须完成。
func validate(password: String) -> Bool {
let capitalLetterRegEx = ".*[A-Z]+.*"
let texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
guard texttest.evaluate(with: password) else { return false }
let numberRegEx = ".*[0-9]+.*"
let texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
guard texttest1.evaluate(with: password) else { return false }
let specialCharacterRegEx = ".*[!&^%$#@()/_*+-]+.*"
let texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)
guard texttest2.evaluate(with: password) else { return false }
return true
}
#3
3
Another alternate solution.
另一个备选的解决方案。
You can do all the check using one regular expression.
您可以使用一个正则表达式来完成所有的检查。
RegExp: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}
You can use this as mention below:
你可以使用如下所述:
//Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
let regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}"
let isMatched = NSPredicate(format:"SELF MATCHES %@", regex).evaluate(with: yourTextField.text)
if(isMatched == true) {
// Do your stuff ..
} else {
// Show Error Message.
}