NSScanner 解析文件
我正在尝试使用 NSScanner 解析 ics 文件(为了解析已将其转换为文本文件)即:calendar file.txt
这里是文本文件的格式:
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
上面是文件的格式。下面是我的代码:
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);
基本上我想存储所有“描述”并将它们存储为变量或数组的一部分。目前,代码将描述输出到控制台,我想将它们保存为变量。有人可以告诉我如何做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否正是您想要的,但是 http://parsekit.com/ 可以让您的工作更容易。它将把源代码放入令牌数组中。您可以迭代每个令牌来过滤“DESCRIPTIONS”令牌,并收集以下令牌进行处理。
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.
尝试使用以下正则表达式而不是使用 NSScanner:
Try using the following regexp instead of using the NSScanner: