2015-12-02 191 views
3

我需要替換字符串這樣正則表達式替換標籤

@@colored:some_text @color:[email protected]@ 

與下面的HTML標籤:

<p style='color:clr;'>some_text</P> 

我寫了一個正則表達式搜索這樣的文字片段,但我很茫然如何做替代。 Here是我的正則表達式

的例子這裏是我的C#代碼的例子,我嘗試做

private string Colored(string data) 
    { 
     var colorMatches = Regex.Matches(data, "@@colored:(.|\n)*? @color:(.*?)@@"); 
     if (colorMatches.Count == 0) 
      return data; 

     var sb = new StringBuilder(); 

     var matches = new List<Match>(); 
     sb.Append(Regex.Replace(data, @"@@colored:(.|\n)*? @color:(.*?)@@", match => 
     { 
      // i don't know how to replace text properly 
     })); 

     return sb.ToString(); 
    } 

請幫我做的文本替換。先謝謝你!

+1

你的意思是[this](https://regex101.com/r/kB1qR7/1)? –

+0

或[this](http://stackoverflow.com/a/1732454/98713)? –

回答

1

Regex.Replace允許您使用$<number>語法來引用捕獲組中定義的值以替代您的正則表達式。您的通話Replace是這樣的:

Regex.Replace(
    data 
, @"@@colored:((?:.|\n)*?) @color:(.*?)@@" 
, @"<p style='$2;'>$1</p>" 
) 

$2(.*?)捕獲組的內容; $1是指((?:.|\n)*?)的內容。請注意,在不創建捕獲組的情況下使用非捕獲括號(?: ...)進行分組。不過,由於回溯,這可能會導致顯着減速,因此您需要非常小心。有關處理問題的方法,請參閱this article

+0

由於產生了回溯步驟的次數,應該儘可能地避免使用'(?:。| \ n)*?'。即使是短弦,也可能容易造成災難性的回溯。 –

1

您需要懶點匹配子模式放到第一個捕獲組(第一組轉義括號):

(?s)@@colored:(.*?) @color:(.*?)@@ 

注意,對於.匹配一個換行符,你需要使用一個單線修改器(內聯(?s)RegexOptions.Singleline標誌)。

,並使用<p style='color:$2;'>$1</p>替換其中$1指的some_text,並$2color

regex demo,這裏是一個IDEONE demo

var str = "some text @@colored:South Africa, officially the Republic of South Africa, is the southernmost country in Africa. It is bounded on the south by 2,798 kilometers of coastline of southern Africa stretching along the South Atlantic and Indian Oceans on the north by the neighbouring countries of Namibia, Botswana and Zimbabwe, and on the east by Mozambique and Swaziland, and surrounding the kingdom of Lesotho.[12] South Africa is the 25th-largest country in the world by land area, and with close to 53 million people, is the world's 24th-most populous nation. @color:[email protected]@ another text"; 
Console.WriteLine(Regex.Replace(str, @"(?s)@@colored:(.*?) @color:(.*?)@@", "<p style='color:$2;'>$1</p>")); 

而且我平時警告:懶點匹配可能會導致代碼執行凍結具有非常大的投入。爲了避免它,使用UNROLL-的環技術:

@@colored:([^ ]*(?: ([email protected]:)[^ ]*)*) @color:([^@]*(?:@([email protected])[^@]*)*)@@ 

此正則表達式有另一個優勢:它不需要一個單線修改,以匹配新行符號。請參閱regex demo #2