I want to validate a string to check if it is alphanumeric and contains "-" and "." with the alphanumeric characters. So I have done something like this to form the regex pattern
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z0-9\\.\\-]"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL valid = [regexTest evaluateWithObject:URL_Query];
App crashes stating that the regex pattern cannot be formed . Can anyone give me a quickfix to what am i doing wrong? Thanks in advance.
[a-zA-Z0-9.\-]-at the end of the character class does not have to be escaped. The regex is valid. However, I'd use^[a-zA-Z0-9.-]+\\zfor this task..within a character class does not need to be escaped. The-may, though, since it does have special meaning withing a character class (e.g[A-Z]).