文本示例:
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 , 并搜索此序列的两次出现。然后,根据我观察到的情况,我将文本字符串中所有出现的 CRLF 更改为空格 here .这不考虑文本每一行中的前导或尾随空格。
我将 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 指令中;在解析文件的过程中可能会生成很多 autoreleased 对象!):
- (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 文件格式起源于法国,输入中看到的逗号是那里使用的小数点分隔符。
关于objective-c - 使用 Objective C 解析 SRT 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14287527/