2010-04-16 135 views
4

回应:Regular Expression to find a string included between two characters while EXCLUDING the delimiters

嗨,我正在寻找一个适用于我的字符串,包括括号中的正则表达式:
分组串用方括号之间的逗号

[1,2,3,4, 5] [abc,ef,g] [0,2,4b,y7]
可以是包括单词,数字,非单词或分开的任何内容。

我希望通过\[(.*?)\] 获得括号中的组,但什么是给我用逗号隔开,这样的结果可能会以下括号和子组串之间的组正则表达式模式?

 
Group1 : 1,2,3,4,5 
Group1: 1 
Group2: 2 
Group3: 3 
Group4: 4 
Group5: 5 

Group2 : abc,ef,g 
Group1: abc 
Group2: ef 
Group3: g 

etc .. 

谢谢您的帮助

+1

有什么特别的原因,你*必须*使用正则表达式? – Amber 2010-04-16 09:49:57

+0

问题看起来似乎比较容易一看,因为答案确实使用字符串操作。如果可能的话,我正在寻找另一种方法来解决我的问题与正则表达式方法。我必须使用正则表达式有一个特别的原因,因为除了使用字符串方法之外,我应该更加明确地了解RegEx。 – Myra 2010-04-16 12:52:50

回答

6

我同意@Dav,你最好在每个方括号组中使用String.Split。

但是,您可以使用一个正则表达式中提取全部数据:

(?:\s*\[((.*?)(?:,(.+?))*)\])+ 

使用这个表情,你将不得不处理所有各groupcaptures来获取所有数据。作为一个例子,在您的字符串运行下面的代码:

var regex = new Regex(@"(?:\s*\[((.*?)(?:,(.+?))*)\])+"); 
var match = regex.Match(@"[1,2,3,4,5] [abc,ef,g] [0,2,4b,y7]"); 

for (var i = 1; i < match.Groups.Count; i++) 
{ 
    var group = match.Groups[i]; 
    Console.WriteLine("Group " + i); 

    for (var j = 0; j < group.Captures.Count; j++) 
    { 
     var capture = group.Captures[j]; 

     Console.WriteLine(" Capture " + j + ": " + capture.Value 
             + " at " + capture.Index); 
    } 
} 

这将产生以下输出:

 
Group 1 
    Capture 0: 1,2,3,4,5 at 1 
    Capture 1: abc,ef,g at 13 
    Capture 2: 0,2,4b,y7 at 24 
Group 2 
    Capture 0: 1 at 1 
    Capture 1: abc at 13 
    Capture 2: 0 at 24 
Group 3 
    Capture 0: 2 at 3 
    Capture 1: 3 at 5 
    Capture 2: 4 at 7 
    Capture 3: 5 at 9 
    Capture 4: ef at 17 
    Capture 5: g at 20 
    Capture 6: 2 at 26 
    Capture 7: 4b at 28 
    Capture 8: y7 at 31 

组1提供了各方括号组的值,第2组给你在每个方括号组和第3组中匹配的第一个项目会为您提供所有后续项目。您将必须查看捕获的索引以确定哪个项目属于每个方括号组。

+0

捕捉群体并合并结果。这是一种方式,看起来像唯一的方式。我接受你的回应。谢谢。 – Myra 2010-04-16 12:54:17

2

你会更好使用你的组String.Split一旦你有支架分隔的群体分割。

1

\[(.*?)\]会告诉你什么是括号之间,但如果添加:

\[(?<NumSequence>.*?)\] 

这将指派一组之后就可以引用。

编辑 然后我会用菲尔的注册防爆如上矿展示了如何分配了一个组。

0

我不认为你问的是可以在一个正则表达式中做什么。您的数据似乎在括号之间有逗号分隔条目的可变数量,并且没有可变数目的捕获组的正则表达式表达式。

3

下面是使用CaptureCollections的另一个选项(在单个正则表达式中执行此操作的唯一方法)。菲尔罗斯的答案在一场比赛中完成了这一切,这场比赛有多场比赛。这样,所有的个人项目捕获都根据它们被发现的支架对进行适当分组。

string s = @"[1,2,3,4,5] [abc,ef,g] [0,2,4b,y7] "; 
Regex r = new Regex(@"\[((?:([^,\[\]]+),?)*)\]"); 
int matchNum = 0; 
foreach (Match m in r.Matches(s)) 
{ 
    Console.WriteLine("Match {0}, Group 1: {1}", ++matchNum, m.Groups[1]); 
    int captureNum = 0; 
    foreach (Capture c in m.Groups[2].Captures) 
    { 
    Console.WriteLine(" Group 2, Capture {0}: {1}", ++captureNum, c); 
    } 
} 

输出:

 
Match 1, Group 1: 1,2,3,4,5 
    Group 2, Capture 1: 1 
    Group 2, Capture 2: 2 
    Group 2, Capture 3: 3 
    Group 2, Capture 4: 4 
    Group 2, Capture 5: 5 
Match 2, Group 1: abc,ef,g 
    Group 2, Capture 1: abc 
    Group 2, Capture 2: ef 
    Group 2, Capture 3: g 
Match 3, Group 1: 0,2,4b,y7 
    Group 2, Capture 1: 0 
    Group 2, Capture 2: 2 
    Group 2, Capture 3: 4b 
    Group 2, Capture 4: y7 
相关问题