Parse subtitle file using regex C#

前端 未结 5 474
南笙
南笙 2021-01-26 19:49

I need to find the number, the in and out timecode points and all lines of the text.

9
00:09:48,347 --> 00:09:52,818
- Let\'s see... what else she\'s got?
-          


        
5条回答
  •  执笔经年
    2021-01-26 19:59

    I think there's two problems with the regex. The first is that the . near the end in (?.+) is not matching newlines. So you could modify it to:

    (?(.|[\r\n])+?)
    

    Or you could specify RegexOptions.Singleline as an option to the regex. The only thing the option does is make the dot match newlines.

    The second problem is that .+ matches as many lines as it can. You can make it non-greedy like:

    (?(.|[\r\n])+?(?=\r\n\r\n|$))
    

    This matches the least amount of text that ends with an empty line or the end of the string.

提交回复
热议问题