文字范例:
1
00:00:00,000 --> 00:00:01,000
This is the first line
2
00:00:01,000 --> 00:00:02,000
This is the second line
3
00:00:02,000 --> 00:00:03,000
This is the last line
在JavaScript中,我肯定会用一个正则表达式来解析它。我只是想知道,这是在Obj C中执行此操作的最佳方法吗?我敢肯定我可以找到一种方法来做到这一点,但是我想以一种适当的方式来做到这一点。
我只需要知道从哪里开始,我很乐意做剩下的事情,但是为了理解起见,我将最终得到这样的东西(伪代码):
NSDictionary
index -> [0-9]+
start -> hh:mm:ss,mmm
end -> hh:mm:ss,mmm
text -> one of the lines of text
在这种情况下,我将在字典中解析三个条目。
最佳答案
背景知识:我编写了一个小应用程序,并创建了一个名为stuff.srt的文件,其中包含捆绑包中的示例。因此,我访问它的方式。
这只是一个快速而肮脏的事情,一个概念证明。请注意,它不会检查结果。实际的应用程序总是检查其结果。如您所见,工作是在-applicationDidFinishLaunching:
方法中进行的(我在Mac OS X中工作,而不是iOS)。
编辑:
已经指出,最初发布的代码无法正确处理多行文本。为了解决这个问题,我利用了SRT files use CRLF as their line breaks这个事实,并搜索了该序列的两次出现。然后,根据观察到的here,将文本字符串中所有出现的CRLF更改为空格。这不考虑文本每一行中的前导或尾随空格。
我将stuff.srt文件的内容更改为此:
1
00:00:00,000 --> 00:00:01,000
This is the first line
and it has a secondary line
2
00:00:01,000 --> 00:00:02,000
This is the second line
3
00:00:02,000 --> 00:00:03,000
This is the last line
and it has a secondary line too
并且代码已作如下修改(我也将所有内容都放入了@autoreleasepool指令中;在解析文件的过程中可能会生成许多自动释放的对象!):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"stuff" ofType:@"srt"];
NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSScanner *scanner = [NSScanner scannerWithString:string];
while (![scanner isAtEnd])
{
@autoreleasepool
{
NSString *indexString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];
NSString *startString;
(void) [scanner scanUpToString:@" --> " intoString:&startString];
// My string constant doesn't begin with spaces because scanners
// skip spaces and newlines by default.
(void) [scanner scanString:@"-->" intoString:NULL];
NSString *endString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];
NSString *textString;
// (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&textString];
// BEGIN EDIT
(void) [scanner scanUpToString:@"\r\n\r\n" intoString:&textString];
textString = [textString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
// Addresses trailing space added if CRLF is on a line by itself at the end of the SRT file
textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// END EDIT
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
indexString, @"index",
startString, @"start",
endString , @"end",
textString , @"text",
nil];
NSLog(@"%@", dictionary);
}
}
}
修改后的输出如下所示:
2013-02-09 16:10:17.727 SRTFileScan[4846:303] {
end = "00:00:01,000";
index = 1;
start = "00:00:00,000";
text = "This is the first line and it has a secondary line";
}
2013-02-09 16:10:17.729 SRTFileScan[4846:303] {
end = "00:00:02,000";
index = 2;
start = "00:00:01,000";
text = "This is the second line";
}
2013-02-09 16:10:17.730 SRTFileScan[4846:303] {
end = "00:00:03,000";
index = 3;
start = "00:00:02,000";
text = "This is the last line and it has a secondary line too";
}
我从今天的阅读中学到的另一件事是:SRT文件格式起源于法国,输入中的逗号是在那里使用的十进制分隔符。