2012-02-13 110 views
1

我想解析由其他人創建的.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] 

可以有從50至100塊像在每個文件中上面的一個。我已經使用NSRegularExpression類在我的應用程序中執行了其他一些解析,但我甚至無法考慮如何完成此操作。

據我所知,每塊由雙線隔開。

+1

你到目前爲止嘗試了什麼? – 2012-02-13 19:19:41

+0

沒有這個,真的。在過去,我曾經工作過的唯一正則表達式是用於簡單的單行項目。 – 2012-02-13 19:23:21

+0

您可能需要NSScanner .... – 2012-02-13 19:23:41

回答

5

嘗試使用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); 

這對我的作品,顯然完成這一過程對所有其他領域。

+0

嗯,我想通過將.rtf文件轉換爲普通文本文件,然後我可以使用'[NSString componentsSeparatedByString:@「\ n \ n」];'方法分解內容。 – 2012-02-13 22:45:33

0

目前,我只能將.rtf文件轉換爲常規文本文件。這使我可以更輕鬆地處理它們。

感謝您的幫助!我將考慮使用NSScanner更優雅地進行操作。