0

我有一个文本文件,内容如下:

+++++ jcguox 4/2/2011 6:19:47 AM

text here

+++++ viveksix 8/24/2012 4:09:17 AM

text again

+++++ viveksix 8/25/2012 11:01:30 AM

----- Vivek Singh 08/25/2012 11:28:12 PM

adding text

+++++ viveksix 8/25/2012 11:02:52 AM

adding more

我想选择所有类型的模式,+++++name date timearraylist条件是:

  • 如果-----name date time appears紧随其后,+++++name date time则不+++++name date time应将其添加到arraylist.
  • 只有当文本出现之后+++++name date time+++++name date time应该添加到arraylist.

现在我能够解析两种模式+++++name date time-----name date time使用Regex.matches.

问题是如何在循环中应用,例如:

foreach(Match m in Regex.Matches(textfile,@"+++++name date time" + @"-----name date time")
{
    if(after +++++name date time text is there)
        arraylist.Add("+++++name date time");
    else if(after +++++name date time -----name date time is there)
        //do not add in arraylist;
}



output for above case is : 

+++++ jcguox 4/2/2011 6:19:47 AM
+++++ viveksix 8/24/2012 4:09:17 AM
+++++ viveksix 8/25/2012 11:02:52 AM
4

2 回答 2

0

看一下这个...

Regex.Match(subjectString, @"\++ (?<author>\w+) (?<date>\d{1,2}/\d{1,2}/\d{4}) (?<time>\d{1,2}:\d{1,2}:\d{1,2} (?:AM|PM))(?!\r\n\r\n-+)")
于 2012-08-26T07:44:36.487 回答
0

尝试这个:

string input = @" i have a text file with content something like this:

     +++++ jcguox 4/2/2011 6:19:47 AM

    text here

    +++++ viveksix 8/24/2012 4:09:17 AM

    text again


    +++++ viveksix 8/25/2012 11:01:30 AM

    ----- Vivek Singh 08/25/2012 11:28:12 PM

    adding text


    +++++ viveksix 8/25/2012 11:02:52 AM

    adding more";

        ArrayList al = new ArrayList();

        MatchCollection mc = Regex.Matches(input, @"\++ (?<author>\w+) (?<date>\d{1,2}/\d{1,2}/\d{4}) (?<time>\d{1,2}:\d{1,2}:\d{1,2} (?:AM|PM))(?!\r\n\r\n-+)");
        foreach (Match m in mc)
        {
            string st = m.NextMatch().ToString();
            if (Regex.Match(st, @"\++ (?<author>\w+) (?<date>\d{1,2}/\d{1,2}/\d{4}) (?<time>\d{1,2}:\d{1,2}:\d{1,2} (?:AM|PM))(?!\r\n\r\n-+)").Length > 0)
            {
                al.Add(st);
            }

        }
于 2012-08-26T08:29:12.117 回答