Regex 以Swift发行正则表达式
我正在使用一个用户名正则表达式,其中唯一可以接受的字符是a-z、a-z、0-9和x。用户名的当前最大长度为18个字符,最小长度为1个。我现在的正则表达式如下 让regexCorrectPattern=“[a-zA-Z0-9}{1,18}$” 我有以下问题。在字符串末尾以外的任何位置添加的特殊字符允许正则表达式通过。比如说 乔恩!失败 !!琼帕斯 j!通行证 下面是我用来测试正则表达式的方法以及调用方法。如有任何意见,将不胜感激 正则表达式测试方法Regex 以Swift发行正则表达式,regex,swift,Regex,Swift,我正在使用一个用户名正则表达式,其中唯一可以接受的字符是a-z、a-z、0-9和x。用户名的当前最大长度为18个字符,最小长度为1个。我现在的正则表达式如下 让regexCorrectPattern=“[a-zA-Z0-9}{1,18}$” 我有以下问题。在字符串末尾以外的任何位置添加的特殊字符允许正则表达式通过。比如说 乔恩!失败 !!琼帕斯 j!通行证 下面是我用来测试正则表达式的方法以及调用方法。如有任何意见,将不胜感激 正则表达式测试方法 func regexTestString(str
func regexTestString(string: String, withPattern regexPattern: String) -> Bool
{
// This method is used for all of the methods below.
do
{
// Create regex and regex range.
let regex = try NSRegularExpression(pattern: regexPattern, options: .CaseInsensitive)
let range = NSMakeRange(0, string.characters.count)
// Test for the number of regex matches.
let numberOfMatches = regex.numberOfMatchesInString(string, options: [], range: range)
// Testing Code.
print(numberOfMatches)
// Return true if the number of matches is greater than 1 and return false if the number of mathces is 0.
return (numberOfMatches == 0) ? false : true
}
catch
{
// Testing Code
print("There is an error in the SignUpViewController regexTestString() method \(error)")
// If there is an error return false.
return false
}
}
func usernameTextFieldDidEndEditing(sender: AnyObject)
{
let usernameText = self.usernameField.text!.lowercaseString
let regexCorrectPattern = "[a-zA-Z0-9_]{1,18}$"
let regexWhitespacePattern = "\\s"
let regexSpecialCharacterPattern = ".*[^A-Za-z0-9].*"
if regexTestString(usernameText, withPattern: regexCorrectPattern)
{
// The regex has passed hide the regexNotificationView
}
else if regexTestString(usernameText, withPattern: regexWhitespacePattern)
{
// The username contains whitespace characters. Alert the user.
}
else if regexTestString(usernameText, withPattern: regexSpecialCharacterPattern)
{
// The username contains special characters. Alert the user.
}
else if usernameText == ""
{
// The usernameField is empty. Make sure the sign up button is disabled.
}
else
{
// For some reason the Regex is false. Disable the sign up button.
}
}
调用方法
func regexTestString(string: String, withPattern regexPattern: String) -> Bool
{
// This method is used for all of the methods below.
do
{
// Create regex and regex range.
let regex = try NSRegularExpression(pattern: regexPattern, options: .CaseInsensitive)
let range = NSMakeRange(0, string.characters.count)
// Test for the number of regex matches.
let numberOfMatches = regex.numberOfMatchesInString(string, options: [], range: range)
// Testing Code.
print(numberOfMatches)
// Return true if the number of matches is greater than 1 and return false if the number of mathces is 0.
return (numberOfMatches == 0) ? false : true
}
catch
{
// Testing Code
print("There is an error in the SignUpViewController regexTestString() method \(error)")
// If there is an error return false.
return false
}
}
func usernameTextFieldDidEndEditing(sender: AnyObject)
{
let usernameText = self.usernameField.text!.lowercaseString
let regexCorrectPattern = "[a-zA-Z0-9_]{1,18}$"
let regexWhitespacePattern = "\\s"
let regexSpecialCharacterPattern = ".*[^A-Za-z0-9].*"
if regexTestString(usernameText, withPattern: regexCorrectPattern)
{
// The regex has passed hide the regexNotificationView
}
else if regexTestString(usernameText, withPattern: regexWhitespacePattern)
{
// The username contains whitespace characters. Alert the user.
}
else if regexTestString(usernameText, withPattern: regexSpecialCharacterPattern)
{
// The username contains special characters. Alert the user.
}
else if usernameText == ""
{
// The usernameField is empty. Make sure the sign up button is disabled.
}
else
{
// For some reason the Regex is false. Disable the sign up button.
}
}
您希望整个字符串仅包含指定的字符,因此只需在模式的开头添加
^
:
^[a-zA-Z0-9{1,18}$
a-z、a-z、0-9和
如何!jon应该通过吗?太棒了!这非常有效。谢谢你的帮助。