1

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/London: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?

Ibz
  • 518
  • 1
  • 8
  • 26

2 Answers2

3

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.

eonil
  • 83,476
  • 81
  • 317
  • 516
2

Try using the following regexp instead of using the NSScanner:

/DESCRIPTION:([^\n]+)\n/is
cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • how would i got about using regexp? also where would i put this regexp sorry to bother u again.. and thanks for answering! – Ibz Mar 07 '11 at 17:04
  • Use RegexKit*Lite* (not the framework, the Lite-version) from here: http://regexkit.sourceforge.net/. It's pretty easy to use ;)! – cutsoy Mar 07 '11 at 17:07
  • If you use the Lite-version than yes it does :)! The framework-version won't work. You just need to link your app to libicore (or something). – cutsoy Mar 07 '11 at 17:10
  • Alright Tim.. Thanks for your time.. it is very much appreciated.. il try that and see how it goes.. – Ibz Mar 07 '11 at 17:12