Objective c 使用NSregularexpression预测
我使用以下正则表达式来匹配字符串中的整个单词Objective c 使用NSregularexpression预测,objective-c,regex,nspredicate,nsregularexpression,Objective C,Regex,Nspredicate,Nsregularexpression,我使用以下正则表达式来匹配字符串中的整个单词 (?:[a-b][A-B][0-9])*test(?:[a-b][A-B][0-9])* //ig 我已经在上测试了这个正则表达式,并使用下面的示例数据按预期工作 示例字符串: these are words !@#$hello@#$$$ abc@123.com z,kakdk test test.sample@56789.com 8x11-1dc pages 123 123 TeSt test_2000 我需要使用这个正则表达式来过滤与使用谓
(?:[a-b][A-B][0-9])*test(?:[a-b][A-B][0-9])* //ig
我已经在上测试了这个正则表达式,并使用下面的示例数据按预期工作
示例字符串:
these are words !@#$hello@#$$$ abc@123.com z,kakdk test test.sample@56789.com 8x11-1dc pages 123 123 TeSt test_2000
我需要使用这个正则表达式来过滤与使用谓词的正则表达式匹配的字符串数组。我使用下面的代码来实现这一点,但我总是得到零输出
NSString *string = [NSString stringWithFormat:@"(?:[a-b][A-B][0-9])*%@(?:[a-b][A-B][0-9])*",searchTerm];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name MATCHES[c] %@",string];
使用以下代码验证了我的正则表达式,效果良好:
NSString *someRegexp = @"(?:[a-b][A-B][0-9])*test(?:[a-b][A-B][0-9])*";
NSString *testString = @"these are words !@#$hello@#$$$ abc@123.com z,kakdk test_2000 test.sample@56789.com 8x11-1dc pages 123 123 TeSt";
NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:someRegexp options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger regExMatches = [regEx numberOfMatchesInString:testString options:0 range:NSMakeRange(0, [testString length])];
请帮帮我。非常感谢你的帮助。提前感谢我已经准备好了一个利用
NSRegularExpression
类的小型解决方案。在此特定实例中,不需要使用NSPredicate
来匹配任意字符串中的文本
main.m
中的完整程序如下所示:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *testString = @"these are words !@#$hello@#$$$ abc@123.com z,kakdk test_2000 test.sample@56789.com 8x11-1dc pages 123 123 TeSt";
NSError *regexError = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\w+"
options:NSRegularExpressionCaseInsensitive
error:®exError];
if (regexError) {
// handle error
NSLog(@"%@", regexError.localizedDescription);
}
[regex enumerateMatchesInString:testString
options:0
range:NSMakeRange(0, testString.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"%@", [testString substringWithRange:result.range]);
}];
}
return 0;
}
2014-08-06 13:39:56.195 NSPredicate-NSRegularExpression[3914:303] these
2014-08-06 13:39:56.197 NSPredicate-NSRegularExpression[3914:303] are
2014-08-06 13:39:56.198 NSPredicate-NSRegularExpression[3914:303] words
2014-08-06 13:39:56.199 NSPredicate-NSRegularExpression[3914:303] hello
2014-08-06 13:39:56.200 NSPredicate-NSRegularExpression[3914:303] abc
2014-08-06 13:39:56.201 NSPredicate-NSRegularExpression[3914:303] 123
2014-08-06 13:39:56.201 NSPredicate-NSRegularExpression[3914:303] com
2014-08-06 13:39:56.202 NSPredicate-NSRegularExpression[3914:303] z
2014-08-06 13:39:56.203 NSPredicate-NSRegularExpression[3914:303] kakdk
2014-08-06 13:39:56.203 NSPredicate-NSRegularExpression[3914:303] test_2000
2014-08-06 13:39:56.204 NSPredicate-NSRegularExpression[3914:303] test
2014-08-06 13:39:56.204 NSPredicate-NSRegularExpression[3914:303] sample
2014-08-06 13:39:56.205 NSPredicate-NSRegularExpression[3914:303] 56789
2014-08-06 13:39:56.205 NSPredicate-NSRegularExpression[3914:303] com
2014-08-06 13:39:56.206 NSPredicate-NSRegularExpression[3914:303] 8x11
2014-08-06 13:39:56.206 NSPredicate-NSRegularExpression[3914:303] 1dc
2014-08-06 13:39:56.207 NSPredicate-NSRegularExpression[3914:303] pages
2014-08-06 13:39:56.207 NSPredicate-NSRegularExpression[3914:303] 123
2014-08-06 13:39:56.208 NSPredicate-NSRegularExpression[3914:303] 123
2014-08-06 13:39:56.208 NSPredicate-NSRegularExpression[3914:303] TeSt
请正确格式化您的帖子,使代码块可读。我在下面添加了一个答案,但如果我误解了问题,或者您需要从字符串中提取特定的单词/短语,请留下评论,我很乐意看一看。