はこのように、NSScanner
を使用してみてください:
NSString *input =
@"[Title]\n"
@"[Type] ([sub type])\n"
@"Level: [CSV list of levels]\n"
@"Components: [CSV list of components]\n"
@"Time: [proprietary time format]\n"
@"Length: [length value]\n"
@"Target: [target text]\n"
@"Dwell: [dwell time in proprietary time format]\n"
@"Saves: [yes/no]\n"
@"Additional Information: [additional information]\n"
@"[notes]\n";
NSString *title, *type, *subType, *level, *components, *time, *length, *target, *dwell, *saves, *additional, *notes;
title = type = subType = level = components = time = length = target = dwell = saves = additional = notes = nil;
NSScanner *scanner = [NSScanner scannerWithString:input];
// read the first line into title...
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&title];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read the first part of the second line into type
[scanner scanUpToString:@" (" intoString:&type];
[scanner scanString:@"(" intoString:nil];
// read the next part of the second line into subType
[scanner scanUpToString:@")" intoString:&subType];
// read the end of the line
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in level
[scanner scanString:@"Level: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&level];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in components:
[scanner scanString:@"Components: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&components];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in time:
[scanner scanString:@"Time: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&time];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in length
[scanner scanString:@"Length: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&length];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// complete for all other metadata
NSLog(@"%@", title);
NSLog(@"%@ (%@)", type, subType);
NSLog(@"%@", level);
NSLog(@"%@", components);
NSLog(@"%@", time);
NSLog(@"%@", length);
NSLog(@"%@", target);
NSLog(@"%@", dwell);
NSLog(@"%@", saves);
NSLog(@"%@", additional);
NSLog(@"%@", notes);
これは私のために動作しますが、明らかに他のすべてのフィールドのためのプロセスを完了します。
これまでに何を試みましたか? –
これは本当に何もありません。過去に私が今までに仕事をしてきた唯一の正規表現は、シンプルで単一のラインアイテムです。 –
あなたはおそらくNSScannerを必要とします.... –