如何处理多行字符串

问题描述:

我有返回与参数和没有parameter..without参数(具有恒定)测试了多string..I've的方法它works..here是函数如何处理多行字符串

public string AppComments() 
     { 
      string teststring = @"Nelly Thomas (Approve) 12/27/2012 8:50 PM - 12/27/2012 8:52 PM 
           (Nelly Thomas) LazyApproval by [email protected] Approved 

           Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
           when an unknown printer took a galley of type and scrambled it to make a type specimen book"; 

       Regex reg = new Regex(@"(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r].+[\n|\r]{2}((?:.|\n)+)", RegexOptions.IgnoreCase | RegexOptions.Multiline); 

       string returnstring = reg.Match(teststring).Groups[1].Value + reg.Match(teststring).Groups[2].Value + reg.Match(teststring).Groups[4].Value.ToString(); 

      return returnstring; 
     } 

但是,当我通过followig文本它永远不会返回任何值..它看起来空白..我猜我传递的内部值没有多行文字?

Nelly Thomas (Approve) 12/27/2012 8:50 PM - 12/27/2012 8:52 PM 
(Nelly Thomas) LazyApproval by [email protected] Approved 

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book 

下面是参数

public string AppComments(string mystring) 
     { 

       Regex reg = new Regex(@"(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r].+[\n|\r]{2}((?:.|\n)+)", RegexOptions.IgnoreCase | RegexOptions.Multiline); 

       string returnstring = reg.Match(mystring).Groups[1].Value + reg.Match(mystring).Groups[2].Value + reg.Match(mystring).Groups[4].Value.ToString(); 

      return returnstring; 
     } 

尝试使用$作为predicat在您的正则表达式的行结束的功能。 RegexOptions.Multiline是没有意义的,如果你不使用^和$

另一种方法:尽量标记行尾可选:

Regex reg = new Regex(@"(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r]??.+([\n|\r]{2})??((?:.|\n)+)", RegexOptions.IgnoreCase);

+0

我不understand..how补充? – user342944

+0

'RegexOptions.Multiline'只改变^和$的行为,所以它们匹配在每一行的开始和结尾(不是整个字符串)。 –

+0

Fruttis可以在我的代码中进行更改并将其作为答案吗? – user342944