2014-09-10 125 views
0

我有以下數據記錄:嵌套花括號內數據的正則表達式?

@INPROCEEDINGS{conf/icsm/Ceccato07, 
    author = {Mariano Ceccato}, 
    title = {Migrating Object Oriented code to Aspect Oriented Programming}, 
    booktitle = {ICSM}, 
    year = {2007}, 
    pages = {497--498}, 
    publisher = {IEEE}, 
    bibdate = {2008-11-18}, 
    bibsource = {DBLP, http://dblp.uni-trier.de/db/conf/icsm/icsm2007.html#Ceccato07}, 
    crossref = {conf/icsm/2007}, 
    owner = {Administrator}, 
    timestamp = {2009.04.30}, 
    url = {http://dx.doi.org/10.1109/ICSM.2007.4362668} 
} 

有沒有一種方法,我可以得到下面的匹配問題,使得每一行是一個單獨的匹配

conf/icsm/Ceccato07 
    author = Mariano Ceccato 
    title = Migrating Object Oriented code to Aspect Oriented Programming 
    booktitle = ICSM 
    year = 2007 
    pages = 497--498 
    publisher = IEEE 
    bibdate = 2008-11-18 
    bibsource = DBLP, http://dblp.uni-trier.de/db/conf/icsm/icsm2007.html#Ceccato07 
    crossref = conf/icsm/2007 
    owner = Administrator 
    timestamp = 2009.04.30 
    url = http://dx.doi.org/10.1109/ICSM.2007.4362668 

這裏是我的圖案,其錯過了第一行匹配,並沒有位擺脫大括號和逗號

string pattern = ".*[{].*}"; 
+0

是的,有...但你嘗試過這麼遠嗎? – 2014-09-10 21:24:23

+1

因爲我不是正則表達式的忠實粉絲,並且因爲你很難理解這一點,所以不會string.Replace()是一個稍微簡單的選項? – user2366842 2014-09-10 21:25:16

+0

我們需要更多的選票,當你告訴我爲什麼,或許我可以通過我的頭腦來獲得它。 – ykh 2014-09-10 21:27:54

回答

1

這個正則表達式,在單線模式,將抓住一切內外側的兩個括號:

(?<=\{).*(?=\}) 

然後你可以使用C#來去掉內部括號:

var regex = new Regex(@"(?<=\{).*(?=\})", RegexOptions.Singleline); 
var match = regex.Match(input); 

return match.Value.Replace("{", string.Empty).Replace("}", string.Empty);