Anand
Anand

Reputation: 864

Regex not working and always detecting match

Please tell me what is wrong with this regex. I need to have only alphabets and no digits at all.

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(?!\\s*$)()[a-zA-Z,\\w,.\\s,-]{2,50}$"
                                                                       options:NSRegularExpressionUseUnicodeWordBoundaries
                                                                         error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:text
                                                    options:0
                                                      range:NSMakeRange(0, [text length])];

if (numberOfMatches == 1) {
    return  nil;
} else {
    return NSLocalizedString(@"name.validation.alert1", nil);
}

Thanks in advance.

Upvotes: 0

Views: 200

Answers (1)

Kamil Wozniak
Kamil Wozniak

Reputation: 445

There is a . in the regex, which allows also a digit. Please reconsider changing regex like this:

^(?=\\S)()[A-Za-z\\s]{2,50}$

It would be nice, if you could paste here some string you evaluate. You can also try your regex online in site like: regexpal.com Cheers.

Upvotes: 1

Related Questions