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.
精彩评论