2014-03-27 85 views
0

我想將SubRip文件內容讀入塊字幕數組中。如何將SubRip文件內容讀入塊字幕數組?

SubRip內容例如:

1 
00:00:00,000 --> 00:00:15,000 
A time for us 

2 
00:00:15,001 --> 00:00:17,001 
someday there'll be 

3 
00:00:17,002 --> 00:00:22,002 
When chains are torn by courage born 

4 
00:00:22,003 --> 00:00:24,003 
Of a love that's free 

SubRip塊格式:

第一行:指數字幕

下聯:間隔時間

第三行:字幕文本

我嘗試:

string subRipContent = ReadTextFileFromUrl(); 
Match[] matches = Regex.Matches(subRipContent, @"^(\d\d)\t(\d\d\:\d\d\:\d\d\:\d\d)") 
          .Cast<Match>() 
          .ToArray(); 

解釋代碼:

我已經使用Regex.Matches讀取來自subRipContent串subrip塊爲Array。但不成功,數組返回長度爲零。

注意:參數subRipContent已經有內容作爲上面的subrip內容示例。

原因:正則表達式格式不正確。

要求:我需要你的幫助來構造一個正則表達式格式來讀取subrip從subRipContent塊到數組中。

結果應該是字符串數組看起來像這樣:

string[] ID = { 
[1 
    00:00:00,000 --> 00:00:15,000 
    A time for us], 
[2 
    00:00:15,001 --> 00:00:17,001 
    someday there'll be], 
[3 
    00:00:17,002 --> 00:00:22,002 
    When chains are torn by courage born]}; 

回答

4

試試這個。它會給出每個字幕的數組。

string subRipContent = ReadTextFileFromUrl(); 
string[] splitData = data.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); 
+1

簡單而優秀,但最好是用'new string [] {Environment.NewLine + Environment.NewLine}'進行分割。此外,這裏不需要'StringSplitOptions.RemoveEmptyEntries'。 – Tarec

+0

太好了。你是在哪裏拿到的?這很簡單,但很完美。爲什麼我不能在第一時間想到它。順便說一句,非常感謝您的幫助。 –