-1

please i need your help. Here i wrote the part of code, and can not find, where is my mistake:

NSString *inputString =@"11111111111";
NSError *error = nil;
NSRegularExpression *regExpression = [NSRegularExpression regularExpressionWithPattern:@"[[a-zA-Z]]*"
                                                                          options:NSRegularExpressionCaseInsensitive error:&error];

NSUInteger numberOfMatches = [regExpression numberOfMatchesInString:inputString
                                                            options:0
                                                              range:NSMakeRange(0, [inputString length])];

NSLog(@"numberOfMatches=%d", numberOfMatches); 
// here shows  "numberOfMatches = 7"

But checked here the result, the answer is incorrect ! http://gskinner.com/RegExr/

So question: where is my mistake ?

kokemomuke
  • 440
  • 6
  • 10
  • How should we know this? You don't tell us what you expect, so we can't know the correct answer. – stema Sep 11 '13 at 06:02
  • I expect that the "inputString" should only be like this: "abs qwer" There can not be any number, only alphabetical words. So for inputString = @"111111" the number of mathes should be Zero. – kokemomuke Sep 11 '13 at 06:11
  • Then my answer should be close to what you want, except that the space is missing in the char class. – stema Sep 11 '13 at 06:15

3 Answers3

2

I don't know why you are getting only 7 matches, I think there should be 12. You don't specify any requirements, so I do a bit of guessing:

The problem is your quantifier *. It matches 0 or more, means [[a-zA-Z]]* it will also match if it finds 0 chars (the empty string) and an empty string will be found before every digit and at the end of the string.

probably it will help you to use the + quantifier, it matches 1 or more. So maybe the regex [a-z]+ is what you want.

Btw. [[a-zA-Z]]* is most probably wrong, I think you want [a-zA-Z]. The other thing is, when you use options:NSRegularExpressionCaseInsensitive you don't need to specify upper and lower case letters in your character class, [a-z] would be fine.

stema
  • 80,307
  • 18
  • 92
  • 121
1

I think you have to change the RegularExpression as [[a-z]*]

MobileDev
  • 994
  • 7
  • 19
0

you can give this a try if your input is 1111...

NSRegularExpression * regExpression = [NSRegularExpression 
regularExpressionWithPattern:@"\\W-?1?[0-9]{2}(\\.[0-9]{1,2})?\\W"
                   options:0
                     error:&error];

for reference you can visit here.

Community
  • 1
  • 1
D-eptdeveloper
  • 2,331
  • 1
  • 13
  • 30
  • The regex pattern i recived from server, and tested that pattern in http://gskinner.com/RegExr/, so everything is correct. The problem is NSRegularExpression is not correct working. – kokemomuke Sep 11 '13 at 05:51