2016-11-11 82 views
-4

我需要一个C#正则表达式,可以找到字符串中的图案。C#正则表达式匹配模式[文字]文字(更多)

这是字符串:

[Tommy] wants 'gum' and [Johny's last name] is ('Smith', 'Doe', 'Black') and [money] is not ('char', 'string') and [Mr Smith's [right hand man]] is ('Johny', 'Tommy') and [Mr Smith] sales 'wine'

我所考虑退出是:

[Johny's last name] is ('Smith', 'Doe', 'Black') [Mr Smith's [right hand man]] is ('Johny', 'Tommy')

任何事情都可能是括号内,有可能是无限量的括号之间的项目。

+3

显示一些研究/努力工作? – Steve

+0

你的意思是比赛应该是(...)吗? –

+0

我们应该用 做“[汤米]想‘胶’anfds] dsfds [dsfdd [佐尼的姓氏]是(‘史密斯’,‘李四’,‘黑’)”? 它是“[dsfdd [佐尼的姓氏]是(‘史密斯’,‘李四’,‘黑’)”或 “[佐尼的姓氏]是(‘史密斯’,‘李四’,‘黑’)”? – MaLiN2223

回答

0

你能否证实这个

public static void Main(string[] args) 
{ 
    var text = 
     @"[Tommy] wants 'gum' and [Johny's last name] is ('Smith', 'Doe', 'Black') and [money] is not ('char', 'string') and [Mr Smith's [right hand man]] is ('Johny', 'Tommy') and [Mr Smith] sales 'wine'"; 
    var pattern = @"((\[[^\]\[]*\])|(\[[^\[\]]*\[[^\[\]]*][^\[\]]*\])) is \(.*?\)"; 
    var regex = new Regex(pattern); 
    var matches = regex.Matches(text); 
    var listOfMatches = new List<string>(); 
    foreach (Match match in matches) 
    { 
     listOfMatches.Add(match.Value); 
    } 
} 

是为你工作?

+0

是的,这是完美的作品。 – yiglas

0
\[[^\[\]]+(\[[^\[\]]+\])?\] is \([^\(\)]+\) 

[汤]想 '胶' 和[佐尼的姓氏]是( '史密斯', '李四', '黑')和[钱]不是( '字符','字符串“)和[史密斯先生[左膀右臂]是(”约翰尼”,‘汤’)和[史密斯先生]销售‘酒’