我有以下字符串:如何以特殊順序提取括號內的文本?
Find string inside brackets [C#.net [C# Only] [PHP and SQl [MySQL] ] ] and [Vb.net] examples.
,我想輸出如下:
1 - [C#.net [C# Only] [PHP and SQl [MySQL] ] ]
2 - [C# Only]
3 - [PHP and SQl [MySQL] ]
4 - [MySQL]
5 - [Vb.net]
我的代碼是:
string regularExpressionPattern = @"\[([^]]*)\]";
string text = "Find string inside brackets [C#.net [C# Only] [PHP and SQl [MySQL] ] ] and [Vb.net] examples.";
Regex re = new Regex(regularExpressionPattern);
int i = 0 ;
foreach (Match m in re.Matches(text))
{
i++;
Console.WriteLine(i + " - " + m.Value);
}
和電流(不正確)的輸出:
1 - [C#.net [C# Only]
2 - [PHP and SQl [MySQL]
3 - [Vb.net]
我忘了提:使用'm.Groups [1] .Value'後來因爲上述正則表達式代碼得到結果第一個捕獲小組。你可以看到我在演示中正好使用了 – Jerry 2015-04-02 07:37:02
我是正規表達式的一週,我可以找到很好的教程我們的示例學習 – Milad 2015-04-02 07:54:08
@Milad你也許可以使用http://www.regular-expressions.info/我個人看那裏,或者在StackOverflow上搜索,因爲機會可能是之前問過的類似問題,或者稍微不同的問題已經解決,我可以在將它適配到我需要的東西后使用該解決方案:) – Jerry 2015-04-02 07:55:57