开发者

regular expressions iphone

How can I c开发者_运维百科hceck on iPhone with regularexpressions NSStrring contain only this chars: a-zA-Z numbers 0-9 or specialchars: !@#$%^&*()_+-={}[]:"|;'\<>?,./


NSCharacterSet *charactersToRemove = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "] invertedSet];
NSString *stringValueOfTextField = [[searchBar.text componentsSeparatedByCharactersInSet:charactersToRemove] 
                                    componentsJoinedByString:@""];

try this::::


For this purposes you can use standard class NSRegularExpression


NSRegularExpression *regex = [NSRegularExpression 
       regularExpressionWithPattern:@"\w[!@#$%^&*()_+-={}\[\]:\"|;'\<>?,./]"
                            options:NSRegularExpressionCaseInsensitive
                              error:&error];

NSUInteger numberOfMatches = [regex numberOfMatchesInString:string
                                                     options:0
                                                       range:NSMakeRange(0, [string length])];

Note that NSRegularExpression is only available on iOS 4 and above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜