如果格式保持不变,则无需使用正则表达式。
尝试使用NSScanner
,尤其是方法scanUpToString:intoString:
和scanString:intoString:
编辑:
这段代码应该是你需要的:
NSString *example = @"#parsed Value1:1a 2013-02-20 10:00:00 UTC Value2=2a Value3=3a Value4=4aKm Value5=A";
NSString *temp, *value1, *date, *value2, *value3, *value4, *value5;
NSScanner *scanner = [NSScanner scannerWithString:example];
// By default, NSScanner will skip whitespace - we don't want it to skip anything here
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@""]];
[scanner scanString:@"#parsed Value1:" intoString:&temp];
[scanner scanUpToString:@" " intoString:&value1];
[scanner scanString:@" " intoString:&temp];
[scanner scanUpToString:@" Value2=" intoString:&date];
[scanner scanString:@" Value2=" intoString:&temp];
[scanner scanUpToString:@" Value3=" intoString:&value2];
[scanner scanString:@" Value3=" intoString:&temp];
[scanner scanUpToString:@" Value4=" intoString:&value3];
[scanner scanString:@" Value4=" intoString:&temp];
[scanner scanUpToString:@" Value5=" intoString:&value4];
[scanner scanString:@" Value5=" intoString:&temp];
value5 = [example substringFromIndex:[scanner scanLocation]];