开发者

regular expressions iphone

开发者 https://www.devze.com 2023-03-31 22:32 出处:网络
How can I c开发者_运维百科hceck on iPhone with regularexpressions NSStrring contain only this chars: a-zA-Z numbers 0-9 or specialchars: !@#$%^&*()_+-={}[]:\"|;\'\\<>?,./ NSCharacterSet *cha

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

精彩评论

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