正则表达式用不同的分隔符分割字符串

时间:2014-08-28 02:40:00

标签: c# regex regex-lookarounds regex-greedy

需要一些正则表达式帮助解决c#中的一些复杂字符串。

我的字符串是:

-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comment 1-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3

我想要的分裂就是:

比赛1:

-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comment 1

第2场比赛:

-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2

第3场:

-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3

我已经根据模式尝试使用Regex.Split()来匹配字符串和破折号( - ),但问题是我希望将其作为分割的一部分。我很好,它给了我字符串和实际评论的单独匹配,以及 - 字符串,但是我得到了重复:

string input = "-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comment 1-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3";
string pattern = "((?<=--)(.*)(?=--------------))";

string[] substrings = Regex.Split(input, pattern);    // Split on hyphens 
int i = 1;
foreach (string match in substrings)
{
    Response.Write("MATCH " + i.ToString() + ":" + string.Format("{0}", match) + "<br>");
    i++;
}

有了这个,我得到的输出是:

    MATCH 1:--
    MATCH 2: [AUG-24-14 11:58 PM (EDT) Mickey Rahman] 
    MATCH 3: [AUG-24-14 11:58 PM (EDT) Mickey Rahman] 
    MATCH 4:-------------- Comment 1 --
    MATCH 5: [AUG-24-14 11:40 PM (EDT) Mickey Rahman] 
    MATCH 6: [AUG-24-14 11:40 PM (EDT) Mickey Rahman] 
    MATCH 7:-------------- Comment 2 --
    MATCH 8: [AUG-22-14 11:51 PM (EDT) Automation User] 
    MATCH 9: [AUG-22-14 11:51 PM (EDT) Automation User] 
    MATCH 10:-------------- TEST 3

2 个答案:

答案 0 :(得分:1)

如果您对使用拆分方法感到满意,这就是我提出的解决方案。

String input   = @"-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comm--ent 1-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3";
string[] parts = Regex.Split(input, @"(?<!\A)(?=-- *\[)");

int i = 1;
foreach (string m in parts) {
         Console.WriteLine("MATCH " + i + ":" + m); ++i;
}

输出

MATCH 1:-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comm--ent 1
MATCH 2:-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2
MATCH 3:-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3

Working Demo

答案 1 :(得分:1)

请尝试以下方法:

string input = "-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comment 1-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3";
string pattern = @"(?=-- \[.+\])";

string[] substrings = Regex.Split(input, pattern).Where(_=>_.Length > 0).ToArray();    // Split on hyphens 
int i = 1;
foreach (string match in substrings)
{
    Console.WriteLine("MATCH " + i.ToString() + ":" + string.Format("{0}", match));
    i++;
}

<强>输出

MATCH 1:-- [AUG-24-14 11:58 PM (EDT)  Mickey Rahman] --------------Comment 1
MATCH 2:-- [AUG-24-14 11:40 PM (EDT)  Mickey Rahman] --------------Comment 2
MATCH 3:-- [AUG-22-14 11:51 PM (EDT)  Automation User] --------------TEST 3