我对 NSRegularExpression 有疑问。我确定这是我的错误,但我无法弄清楚。我有这个代码:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern: nextModelName
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex enumerateMatchesInString:objTmplName options:0 range:NSMakeRange(0, [objTmplName length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
NSLog(@"objTmplName:%@ nextModelName:%@",objTmplName,nextModelName);
}];
输出是:
2012-07-30 10:59:48.779 MyApp[8470:707] objTmplName:yaoygo nextModelName:\w*
2012-07-30 10:59:48.799 MyApp[8470:707] objTmplName:yaoygo nextModelName:yo*
2012-07-30 10:59:48.808 MyApp[8470:707] objTmplName:yaoygo nextModelName:yaoygo
为什么它进入 whenobjTmplName = yaoygo
和nextModelName:yo*
?
我想要做的是当我的 objTmplName 以“yo ...”开头时它会是真的,否则这应该是假的。例如:
yooygo TRUE
yaygo FALSE
yayoyoyo FALSE