Objective-c正则表达式检查两个数字的序列

时间:2013-11-01 16:40:05

标签: objective-c regex

似乎无法弄清楚如何检查字符串,以便不允许连续使用相同的两个字符。

我不希望任何人能够使用“00”提交数据。

1 个答案:

答案 0 :(得分:1)

仅仅是:

(\d)\1+

\d匹配任何数字,\1+匹配第一位匹配的内容,当它出现多次时。


虽然与您的评论有关,但检查起来要容易得多:

if ([expiryDate rangeOfString:@"00"].location != NSNotFound)
{
    //Invalid date
}

甚至可能更有效:

NSArray *components = [expiryDate componentsSeparatedByString:@"/"];
int month = [components[0] intValue];
int year = [components[1] intValue];
NSAssert(month > 0 && month <= 12, @"Invalid Month");
NSAssert(year >= 13 /*current year*/ /* (optionally) && year < 20 (or some other future year)*/, @"Invalid year");