2011-11-21 139 views
1

我有一些文字看起來像這樣:正則表達式C#,匹配組

My arbitrary content.... 

```c# 
public class Foo 
{ 
    //Some code 
} 
``` 
My other arbitrary content.... 

我想找到所有塊開始:

```c# 

和結尾:

``` 

並用這些開始和結束標記之間的內容替換它,以便結果變爲:

My arbitrary content.... 

public class Foo 
{ 
    //Some code 
} 

My other arbitrary content.... 

那會怎麼樣?

回答

4

這聽起來像你只是想刪除三重反引號樣式標記,並保持文檔的其餘部分不變。

text = Regex.Replace(
     text, 
     @"^```c#\r?\n(.*?)```\r?\n", "$1", 
     RegexOptions.Singleline | RegexOptions.Multiline); 

看到它聯機工作:ideone

+0

不存在需要被^開始和結束$,因爲它可能是多塊? – Daniel

+0

@Daniel:查看更新的答案和鏈接。 –