2012-12-21 56 views
0

我想寫一個正則表達式來將下列字符串拆分成段。.NET正則表達式來檢測和拆分字符串

#if [Customer.OrderQuantity] > 5 #then 1000 #if [Customer.OrderQuantity] < 5 #then 500 #else 100 


1st Segment: #if [Customer.OrderQuantity] > 5 #then 1000 
2nd Segment: #if [Customer.OrderQuantity] < 5 #then 500 
3rd Segment: #else 100 

您的建議非常感謝!

+1

我完全不知道爲什麼這篇文章被拒絕。如果你能發表評論會更有幫助。 –

+3

不是我,但我認爲downvoter希望你表現出一些努力。像,你有什麼嘗試? – leppie

+1

沒有必要留下評論,這是顯而易見的,這篇文章顯示沒有努力,只是要求解決方案(你寫了建議)。 [你有什麼嘗試?](http://www.whathaveyoutried.com) – stema

回答

4

這是產生輸出你想

string source = "#if [Customer.OrderQuantity] > 5 #then 1000 #if [Customer.OrderQuantity] < 5 #then 500 #else 100"; 

string[] result = Regex.Split(source, @"\s*(?=#(?:if|else))"); 

foreach (string a in result) { 
    Console.WriteLine(a); 
} 

但我在一些點猜測,我不知道,如果這是在做你想要的其他案件。

+1

我會在前方放置空間,所以它不會落後,但是,這就是我的想法。 – Rawling

+0

@Rawling,好主意,謝謝,將它添加到我的解決方案中。 – stema

+0

@stema ...謝謝讓我試試吧 –

相關問題