Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
Objective c 使用NSRegularExpression仅验证字符串或数字_Objective C_Ios_Ipad - Fatal编程技术网

Objective c 使用NSRegularExpression仅验证字符串或数字

Objective c 使用NSRegularExpression仅验证字符串或数字,objective-c,ios,ipad,Objective C,Ios,Ipad,在IPad中,我有两个文本字段,一个只接受字符串 而其他文本字段只接受整数 因此,我可以使用NSRegularExpression验证它们吗?如果您使用的是RegExKit库 [_yourString isMatchedByRegex:@"\\d+"]; 您可以使用NSPredicate来实现您的目的 匹配数字: NSString *number = @"12345"; NSString *regex = @"[0-9]*"; NSPredicate *predicate = [NSPredi

在IPad中,我有两个文本字段,一个只接受字符串 而其他文本字段只接受整数


因此,我可以使用NSRegularExpression验证它们吗?

如果您使用的是RegExKit库

[_yourString isMatchedByRegex:@"\\d+"];
您可以使用NSPredicate来实现您的目的

匹配数字:

NSString *number = @"12345";
NSString *regex = @"[0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL match = [predicate evaluateWithObject:number]);
NSString *string = @"Hello";
NSString *regex = @"[a-z]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[C] %@", regex];
BOOL match = [predicate evaluateWithObject:string]);
匹配字符串:

NSString *number = @"12345";
NSString *regex = @"[0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL match = [predicate evaluateWithObject:number]);
NSString *string = @"Hello";
NSString *regex = @"[a-z]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[C] %@", regex];
BOOL match = [predicate evaluateWithObject:string]);

在上述正则表达式中,“[a-z]*”我省略了大写字符
“A-Z”
,因为我在谓词中使用了
匹配[C]
,其中
C
表示大小写不敏感。

这个答案有什么意义?(OP特别说明了NSRegularExpression。)