Artur Gurgul
Artur Gurgul

Reputation: 15

regular expressions iphone

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

Upvotes: 0

Views: 680

Answers (3)

apouche
apouche

Reputation: 9973

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.

Upvotes: 0

Mayur Bhola
Mayur Bhola

Reputation: 735

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

try this::::

Upvotes: 2

Nekto
Nekto

Reputation: 17877

For this purposes you can use standard class NSRegularExpression

Upvotes: 0

Related Questions