我对正则表达式很陌生,我正在努力学习它。
这是我的字符串:
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
我想将它拆分为一个如下所示的数组:
@[@"Mozzila", @"4.0", @"compatible", @"MSIE 5.0", @"Windows NT", @"DigExt"];
这是我试过的代码:
NSString *expression = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern: @"([a-zA-Z]+)/([1-9.]+) \(([a-z]+); ([a-zA-Z .]+); ([a-zA-Z ]+); ([a-zA-Z]+)\)" options:NSRegularExpressionCaseInsensitive error:nil];
options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [testExpression matchesInString:expression
options:0
range:NSMakeRange(0, [expression length])];
NSLog(@"%@",matches);
还尝试过:
[testExpression enumerateMatchesInString:expression
options:0
range:NSMakeRange(0, [expression length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"Value: %@", [expression substringWithRange:[result rangeAtIndex:1]]);
}];
并且:
NSRegularExpression *testExpression = [NSRegularExpression
regularExpressionWithPattern: @"(\w+)/(\w+) \((\w+);([\w .]+); ([\w ]+); (\w+)\)" options:NSRegularExpressionCaseInsensitive
error:nil];
但是日志是空的。我究竟做错了什么?