开发者

NSScanner to parse file

开发者 https://www.devze.com 2023-02-15 13:21 出处:网络
I am trying to use NSScanner to parse an ics file (that for the sake of parsing has been converted to a text file) i.e: calendar file.txt

I am trying to use NSScanner to parse an ics file (that for the sake of parsing has been converted to a text file) i.e: calendar file.txt

here is the format of the text file:

BEGIN:VEVENT  
DTSTAMP:20101129T061152Z  
UID:101139897313172011030314:00  
SUMMARY:14:00 - SYSI30251 - CB100 - SEM B  
DESCRIPTION:14:00 - 15:00, SYSI30251 - Module Name<br />Group: B <b>Seminar with Lecturer in room(s) (Clif) Computing Bldg 100  
DTSTART;TZID=Europe/London:20110303T140000  
DTEND;TZID=Europe/London:20110303T150000  
SEQUENCE:2  
END:VEVENT  
BEGIN:VEVENT  
DTSTAMP:20101129T061152Z  
UID:1011558905160182011030315:00  
SUMMARY:15:00 - COMP30251 - CFL015 - LEC    
DESCRIPTION:15:00 - 16:00, COMP30251 - Project Management<br /> Lecture with Lecturer in room(s) (Clif) Centre For Learning 015  
DTSTART;TZID=Europe/Lond开发者_开发技巧on:20110303T150000  
DTEND;TZID=Europe/London:20110303T160000  
SEQUENCE:2  
END:VEVENT  

The above is a the format of the file. Below is my code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"180946_icalfile" ofType:@"txt"];
    NSString *fileComponents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *lines = [fileComponents componentsSeparatedByString:@"\n"];


    NSEnumerator *nse = [lines objectEnumerator];
    NSString *mod;
    while(fileComponents = [nse nextObject]) {
        NSString *stringBetweenBrackets = nil;
        NSScanner *scanner = [NSScanner scannerWithString:fileComponents];
        [scanner scanUpToString:@"," intoString:nil];
        [scanner scanString:@"" intoString:nil];
        [scanner scanUpToString:@"DTSTART" intoString:&stringBetweenBrackets];

        NSLog(@"%@", stringBetweenBrackets);

Basically I want to store all the 'DESCRIPTIONS' and store them as variables or as part of an array. Currently, the code outputs the descriptions onto the console and i would like to save them as variables. Could someone show me how this could be done?


I can't sure that this is exactly what you want, however http://parsekit.com/ can make your work more easier. It will make source code into token array. And you can iterate each tokens to filter "DESCRIPTIONS" token, and collect following tokens to process.


Try using the following regexp instead of using the NSScanner:

/DESCRIPTION:([^\n]+)\n/is
0

精彩评论

暂无评论...
验证码 换一张
取 消