NSScanner 解析文件

发布于 2024-10-20 14:21:23 字数 1750 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-10-27 14:21:23

我不确定这是否正是您想要的,但是 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.

清晨说晚安 2024-10-27 14:21:23

尝试使用以下正则表达式而不是使用 NSScanner:

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

Try using the following regexp instead of using the NSScanner:

/DESCRIPTION:([^\n]+)\n/is
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文