Objective c 解析任意文本数据格式

Objective c 解析任意文本数据格式,objective-c,ios,regex,cocoa-touch,nsregularexpression,Objective C,Ios,Regex,Cocoa Touch,Nsregularexpression,我正在尝试解析由其他人创建的.rtf文件,但我无法控制文件内容或格式。文件中有几个块,每个块都有一组我需要获取的信息。每个区块的设置如下所示: [Title] [Type] ([sub type]) Level: [CSV list of levels] Components: [CSV list of components] Time: [proprietary time format] Length: [length value] Target: [target text] Dwell: [

我正在尝试解析由其他人创建的
.rtf
文件,但我无法控制文件内容或格式。文件中有几个块,每个块都有一组我需要获取的信息。每个区块的设置如下所示:

[Title]
[Type] ([sub type])
Level: [CSV list of levels]
Components: [CSV list of components]
Time: [proprietary time format]
Length: [length value]
Target: [target text]
Dwell: [dwell time in proprietary time format]
Saves: [yes/no]
Additional Information: [additional information]
[notes]
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);
每个文件中可能有50到100个类似上面的块。我已经使用了
NSRegularExpression
类在我的应用程序中执行了一些其他解析,但我甚至无法思考如何完成这一任务


据我所知,每个块由一条双线分隔。

尝试使用
NSScanner
,如下所示:

[Title]
[Type] ([sub type])
Level: [CSV list of levels]
Components: [CSV list of components]
Time: [proprietary time format]
Length: [length value]
Target: [target text]
Dwell: [dwell time in proprietary time format]
Saves: [yes/no]
Additional Information: [additional information]
[notes]
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
,如下所示:

[Title]
[Type] ([sub type])
Level: [CSV list of levels]
Components: [CSV list of components]
Time: [proprietary time format]
Length: [length value]
Target: [target text]
Dwell: [dwell time in proprietary time format]
Saves: [yes/no]
Additional Information: [additional information]
[notes]
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);

这对我来说很有效,显然完成了所有其他字段的处理。

目前,我可以将.rtf文件转换为常规文本文件。这使我能够更轻松地处理它们


谢谢你的帮助!我将研究如何使用NSScanner更优雅地执行此操作。

目前,我可以将.rtf文件转换为常规文本文件。这使我能够更轻松地处理它们


谢谢你的帮助!我将研究如何使用NSScanner更优雅地完成这项工作。

到目前为止,您尝试了什么?真的没有。在我的过去,我唯一能用的正则表达式是简单的单行项目。你可能需要NSScanner…如果我能让别人给我一个起点,那可能会有很大帮助。你到目前为止做了什么?没有什么,真的。在我过去,我唯一能使用的正则表达式是用于简单的单行项目。你可能需要NSScanner…如果我能让别人给我一个起点,那可能会有很大帮助。我发现我可以将.rtf文件转换为普通文本文件,然后,我可以使用
[NSString componentsSeparatedByString:@“\n\n”]分解的内容方法。我发现我可以将.rtf文件转换为普通文本文件,然后使用
[NSString componentsSeparatedByString:@“\n\n”]分解其中的内容方法。