Chr*_*ien 0 regex iphone objective-c ios nsregularexpression
我需要在Objective C中使用正则表达式接受male或者female只接受..应该是case-Insensitive接受更低和更高的手段.我经历了苹果文档.. 并在代码下面实现但不工作...
- > 男性或女性只能被接受
-(BOOL)genderValidate:(NSString *)string{
NSError *error=nil;
NSRegularExpression *regex =[NSRegularExpression regularExpressionWithPattern:@"\\b (m|f)(a|e)(l|m)(e|a)(l)(e) \\b" options:(NSRegularExpressionCaseInsensitive) error: &error];
NSUInteger numberofmatches =[regex numberOfMatchesInString:string options:0 range:NSMakeRange(0,[string length])];
return numberofmatches;
}
Run Code Online (Sandbox Code Playgroud)
为什么要使用正则表达式?把事情简单化:
- (BOOL)genderValidate:(NSString *)string {
string = [string lowercaseString];
return [string isEqualToString:@"male"] || [string isEqualToString:@"female"];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
407 次 |
| 最近记录: |