2017-01-11 68 views
-1

我要让这样的文字:过滤词和数字文本文件

203 
00:16:38,731 --> 00:16:41,325 
Happy Christmas. 
your arse I pray God it's our last. 

是这样的变量:

Int section : 203 
String start_time : 00:16:38,731 
String end_time : 00:16:41,325 
String Content : 
Happy Christmas. 
your arse I pray God it's our last. 

在我的搜索,我发现使用正则表达式,但我不能使用它。这就是我发现它可以帮助理解我的意思:

Regex unit = new Regex(@"(?<sequence>\d+)\r\n(?<start>\d{2}\:\d{2}\:\d{2},\d{3}) --\> " + @"(?<end>\d{2}\:\d{2}\:\d{2},\d{3})\r\n(?<text>[\s\S]*?\r\n\r\n)", RegexOptions.Compiled | RegexOptions.ECMAScript); 

这应该与字幕文件升 我怎么做工作?

谢谢:)

+0

尝试'VAR解析度= unit.Replace(S,“内部部分:$ {序列} \ nString START_TIME:$ {开始} \ nString END_TIME :$ {end} \ nString Content:\ n $ {text}“);' –

回答

0

尝试以下操作:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication41 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string input = 
       "Int section : 203\n" + 
       "String start_time : 00:16:38,731\n" + 
       "String end_time : 00:16:41,325\n" + 
       "String Content :\n" + 
       "Happy Christmas.\n" + 
       "your arse I pray God it's our last.\n"; 

      string pattern = 
       @"Int section :\s+(?'section'\d+)\s+" + 
       @"String start_time :\s+(?'start'[\d:,]+)\s+" + 
       @"String end_time :\s+(?'end'[\d:,]+)\s+" + 
       @"String Content :(?'content'[^$]+)"; 

      Match match = Regex.Match(input, pattern, RegexOptions.Multiline); 
      Console.WriteLine(match.Groups["section"].Value.Trim()); 
      Console.WriteLine("{0} --> {1}", match.Groups["start"].Value.Trim(), match.Groups["end"].Value.Trim()); 
      Console.WriteLine(match.Groups["content"].Value.Trim()); 
      Console.ReadLine(); 
     } 
    } 

} 
+0

感谢您的时间..但您的输入与我的不同......我有字幕文件..这项工作与单词的部分..我的文字必须是这样的:203 00:16:38,731 - > 00:16:41,325 圣诞快乐。 你的屁股我祈祷上帝这是我们的最后一次。 –

+0

它看起来像是有时间的空间,因此:[\ d :,]到:[\ d \ s:,] – jdweng

+0

谢谢你激励我解决它 –