Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSRegularExpression工作不正常_Iphone_Ios_Objective C_Regex_Nsregularexpression - Fatal编程技术网

Iphone NSRegularExpression工作不正常

Iphone NSRegularExpression工作不正常,iphone,ios,objective-c,regex,nsregularexpression,Iphone,Ios,Objective C,Regex,Nsregularexpression,我需要你的帮助。 在这里我写了部分代码,找不到,哪里是我的错误: NSString *inputString =@"11111111111"; NSError *error = nil; NSRegularExpression *regExpression = [NSRegularExpression regularExpressionWithPattern:@"[[a-zA-Z]]*"

我需要你的帮助。 在这里我写了部分代码,找不到,哪里是我的错误:

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"
但是检查这里的结果,答案是不正确的!


所以问题是:我的错误在哪里?

如果您的输入是1111,您可以尝试一下

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

作为参考,你可以访问

我不知道为什么你只得到7场比赛,我想应该有12场。您没有指定任何要求,因此我做了一些猜测:

问题是你的量词*。它匹配0或更多,意味着[[a-zA-Z]]*如果找到0个字符,它也会匹配空字符串,并且在每个数字之前和字符串末尾都会找到一个空字符串

可能它会帮助你使用+量词,它匹配1或更多。所以也许正则表达式[a-z]+就是你想要的


顺便说一句,[[a-zA-Z]]*很可能是错的,我想你想要[a-zA-Z]。另一件事是,当您使用选项:nsregularexpressioncasesensitive时,您不需要在字符类中指定大写和小写字母,[a-z]就可以了。

我认为您必须将正则表达式更改为[[a-z]*]

我从服务器收到的正则表达式模式,并在中测试了该模式,因此一切都是正确的。问题是NSRegularExpression无法正常工作。我们如何知道这一点?您没有告诉我们您期望的是什么,因此我们无法知道正确的答案。我希望输入字符串应该是这样的:abs qwer不能有任何数字,只能是字母单词。因此,对于inputString=@111111,数学数应该是零。那么我的答案应该接近你想要的,只是char类中缺少空格。regularExpressionWithPattern:@[[a-zA-Z]]*,那么我的错误在哪里呢?@kokemomuke一旦将@[[a-zA-Z]*更改为@[[a-Z]*],然后检查