2016-11-11 32 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 \([^\(\)]+\) 

[湯]想 '膠' 和[佐尼的姓氏]是( '史密斯', '李四', '黑')和[錢]不是( '字符','字符串「)和[史密斯先生[左膀右臂]是(」約翰尼」,‘湯’)和[史密斯先生]銷售‘酒’