NSPredicate:如何在过滤器中插入特殊字符(@ $&)

时间:2012-08-20 17:47:31

标签: iphone ios ios5 nspredicate nspredicateeditor

快速提问,我正在使用NSPredicate验证密码,我已将此代码用于用户名:

-(BOOL)isUserValid:(NSString *)checkString{
    NSString       *filter = @"[A-Z0-9a-z]{5,40}";
    NSPredicate *evaluator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
    return [evaluator evaluateWithObject:checkString];
}

但是现在在密码上我想让用户使用特殊字符,例如“@”,“$”,“&”,“*”。

我该怎么做?

1 个答案:

答案 0 :(得分:4)

我不知道您究竟需要多少字符或指定字符才能允许用户使用。但是,根据您的问题示例,如果您只允许用户@$&*个字符。请参阅以下代码。

-(BOOL)isUserValid:(NSString *)checkString{
    NSString       *filter = @"[A-Z0-9a-z@$&*]{5,40}";
    NSPredicate *evaluator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
    return [evaluator evaluateWithObject:checkString];
}

以下过滤器代码是流行的密码expaysion之一。字母数字字符并选择特殊字符。密码也不能以数字,下划线或特殊字符开头,并且必须至少包含一个数字。

  

匹配password1 | pa $$ word2 | pa!@#$ 3 3

     

非匹配密码| 1stpassword | $密码#

-(BOOL)isUserValid:(NSString *)checkString{
        NSString       *filter = @"^(?=[^\\d_].*?\\d)\\w(\\w|[!@#$%]){5,40}";
        NSPredicate *evaluator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
        return [evaluator evaluateWithObject:checkString];
    }

更多关于密码的表达式过滤器。请访问网站:regexlib_password

以下参考是您在工作时的帮助。并记住关于角色和括号的以下表达。

Character Classes

.   Matches any character except newline. Will also match newline if single-line mode is enabled.
\s  Matches white space characters.
\S  Matches anything but white space characters.
\d  Matches digits. Equivalent to [0-9].
\D  Matches anything but digits. Equivalent to [^0-9].
\w  Matches letters, digits and underscores. Equivalent to [A-Za-z0-9_].
\W  Matches anything but letters, digits and underscores. Equivalent to [^A-Za-z0-9_].
\xff    Matches ASCII hexadecimal character ff.
\x{ffff}    Matches UTF-8 hexadecimal character ffff.
\cA Matches ASCII control character ^A. Control characters are case insensitive.
\132    Matches ASCII octal character 132.

Bracket Expressions

[adf?%] Matches characters a or d or f or ? or %.
[^adf]  Matches anything but characters a, d and f.
[a-f]   Match any lowercase letter between a and f inclusive.
[A-F]   Match any uppercase letter between A and F inclusive.
[0-9]   Match any digit between 0 and 9 inclusive. Does not support using numbers larger than 9, such as [10-20].

[:upper:]   Matches uppercase letters. Equivalent to A-Z.
[:lower:]   Matches lowercase letters. Equivalent to a-z.
[:alpha:]   Matches letters. Equivalent to A-Za-z.
[:alnum:]   Matches letters and digits. Equivalent to A-Za-z0-9.
[:ascii:]   Matches ASCII characters. Equivalent to \x00-\x7f.
[:word:]    Matches letters, digits and underscores. Equivalent to \w.
[:digit:]   Matches digits. Equivalent to 0-9.
[:xdigit:]  Matches characters that can be used in hexadecimal codes. Equivalent to A-Fa-f0-9.
[:punct:]   Matches punctuation.
[:blank:]   Matches space and tab. Equivalent to [ \t].
[:space:]   Matches space, tab and newline. Equivalent to \s.
[:cntrl:]   Matches control characters. Equivalent to [\x00-\x1F\x7F].
[:graph:]   Matches printed characters. Equivalent to [\x21-\x7E].
[:print:]   Matches printed characters and spaces. Equivalent to [\x21-\x7E ].