2009-04-21 5 views

답변

5

MatchEvaluator이 수행 할 수 있습니다 match 변수도 함께 Match 등에 액세스 할 수 있는지

string input = "FindbcFinddefFind", pattern = "Find"; 
int i = 1; 
string replaced = Regex.Replace(input, pattern, match => "REPLACE" + i++); 

주 당신이를 사용해야합니다 C# 2.0 익명의 메서드가 아니라 람다 (동일한 효과) - 이것과 함께 표시하려면 Match :

string input = "FindbcFinddefFind", pattern = "Find"; 
int i = 1; 
string replaced = Regex.Replace(input, pattern, delegate(Match match) 
{ 
    string s = match.Value.ToUpper() + i; 
    i++; 
    return s; 
}); 
3

MatchEvaluator을 사용하는 오버로드를 사용하고 위임 구현 내에서 사용자 지정 대체 문자열을 제공하면 모든 대체를 한 번에 수행 할 수 있습니다. 예를 들어

:

var str = "aabbccddeeffcccgghhcccciijjcccckkcc"; 
var regex = new Regex("cc"); 
var pos = 0; 
var result = regex.Replace(str, m => { pos++; return "Replace" + pos; }); 
관련 문제