匹配日期后跟字符串的模式

时间:2022-09-13 07:48:51

I have a string with the repeating pattern of the form

我有一个字符串与表单的重复模式

MM/DD/YYYY (FirstName LastName) Status Update: blah blah blah blah

MM / DD / YYYY(FirstName LastName)状态更新:blah blah blah blah

E.G.

例如。

string test = "11/01/2011 (Joe Bob) Status Update: Joe is the collest guy on earfth 08/07/2010 (Rach Mcadam) Status Update: whatever I dont care 06/28/2009 (Some Guy) Status Update: More junk and note how I end there's not gonna be another date after me"

How can I group match this so as to have Date, Name, and Status update for each match?

如何对此进行分组以便为每个匹配更新日期,名称和状态?

I tried

我试过了

        string datePattern = "\\d{1,2}/\\d{1,2}/\\d{0,4}";
        string personPattern = "\\(\\w*\\)";
        Regex regex = new Regex("(" + datePattern + ") (" + personPattern + ") (.*)");
        MatchCollection matches = regex.Matches(test);
        foreach (Match match in matches)
        {
            Console.WriteLine("##Match Found##");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine(match.Groups[0]);//full text
            Console.WriteLine("");
            Console.WriteLine(match.Groups[1]);//date only
            Console.WriteLine("");
            Console.WriteLine(match.Groups[2]);//person
            Console.WriteLine("");
            Console.WriteLine(match.Groups[3]);//note
        }

It's pulling back nothing at this point.

在这一点上,它什么都没有回来。

1 个解决方案

#1


3  

Spaces aren't included in \w, so \w* will not match Joe Bob. Try changing personPattern to "\\([ \\w]*\\)".

空格不包含在\ w中,因此\ w *与Joe Bob不匹配。尝试将personPattern更改为“\\([\\ w] * \\)”。

It also looks like your regex is too greedy, because the .* at the end will match the rest of the string, instead of stopping at the next date. Try changing your regex to the following:

它看起来像你的正则表达式太贪心,因为。*末尾将匹配字符串的其余部分,而不是在下一个日期停止。尝试将正则表达式更改为以下内容:

Regex regex = new Regex("(" + datePattern + ") (" + personPattern + ") (.*?(?=$|" + datePattern + "))");

#1


3  

Spaces aren't included in \w, so \w* will not match Joe Bob. Try changing personPattern to "\\([ \\w]*\\)".

空格不包含在\ w中,因此\ w *与Joe Bob不匹配。尝试将personPattern更改为“\\([\\ w] * \\)”。

It also looks like your regex is too greedy, because the .* at the end will match the rest of the string, instead of stopping at the next date. Try changing your regex to the following:

它看起来像你的正则表达式太贪心,因为。*末尾将匹配字符串的其余部分,而不是在下一个日期停止。尝试将正则表达式更改为以下内容:

Regex regex = new Regex("(" + datePattern + ") (" + personPattern + ") (.*?(?=$|" + datePattern + "))");