OGeek|极客世界-中国程序员成长平台

标题: ios - 确认密码的正则表达式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:36
标题: ios - 确认密码的正则表达式

我的密码正则表达式是

- (NSString *) getRegularExpressionNSString *)extraWords
{
    /*
     *  ^$          Empty string
     *  |           Or
     *  ^           Start of a string
     *  []          Explicit set of characters to match
     *  ^[:space:]  Matches any non-white-space character - equivalent to \S
     *  {6,30}      6-20 characters
     *  $           End of a string
     */
    return @"^$|^[^[:space:]]{6,20}$";
}

如何编写包含上述要求的确认密码正则表达式,它也等于我的密码字符串。谁能帮帮我?



Best Answer-推荐答案


在你的 shouldChangeCharactersInRange 中你可以这样做

- (BOOL)textFieldUITextField *)textField shouldChangeCharactersInRangeNSRange)range replacementStringNSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^$|^[^[:space:]]{6,20}$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression 
                                                                           options:NSRegularExpressionCaseInsensitive 
                                                                            error:nil];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];        
    if (numberOfMatches == 0)
        return NO;        


    return YES;
}

关于ios - 确认密码的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403292/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4