0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anand
  • 864
  • 10
  • 31

1 Answers1

1

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.

Kamil Wozniak
  • 430
  • 3
  • 7
  • nope, still the same problem. getting no error for the following : test1 – Anand Nov 11 '13 at 09:10
  • Please post at least one valid expression/string for regex here, which should give no error for You. – Kamil Wozniak Nov 11 '13 at 09:20
  • this regex works fine on javascript on website link http://www.regular-expressions.info/javascriptexample.html. But it seems Objective C specific issue. – Anand Nov 11 '13 at 09:22
  • The correct string : Peter Parker. Incorrect String : Peter1 Parker – Anand Nov 11 '13 at 09:23
  • I'll try to make one regex for You, in the mean time please see the site: http://stackoverflow.com/questions/9276246/how-to-write-regular-expressions-in-objective-c-nsregularexpression – Kamil Wozniak Nov 11 '13 at 09:25
  • And this site also may be helpful: [link](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html) – Kamil Wozniak Nov 11 '13 at 09:28
  • well, it seems to be working (hopefully in all the cases). Thanks a lot @Kamil :) – Anand Nov 11 '13 at 09:48
  • 1
    Maybe this regex will work for You? `^(?=\S)()[A-Za-z\s]{2,50}$` – Kamil Wozniak Nov 11 '13 at 09:49
  • In the previous one I forgot that You may not want to have spaces at the beginning of the words, right? – Kamil Wozniak Nov 11 '13 at 09:50
  • 1
    Yes...I do not want spaces at the beginning. ^(?=\S)()[A-Za-z\s]{2,50}$ works as expected (excluding the spaces at the beginning) – Anand Nov 11 '13 at 09:51
  • So please check regex from the answer, I edited it. Thanks to the post: http://stackoverflow.com/questions/446108/regular-expression-for-no-blank-space-b-w-text Cheers. – Kamil Wozniak Nov 11 '13 at 09:53
  • 1
    Yes, it works. In obj-c its NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(?=\\S)()[A-Za-z\\s]{2,50}$" options:NSRegularExpressionUseUnicodeWordBoundaries error:&error]; – Anand Nov 11 '13 at 09:58