public string regexDate(string str)
{
return Regex.Replace(str,"'(?<year>//d{4})-(?<month>//d{1,2})-(?<day>//d{1,2})'","To_Date('${year}-${month}-${day}','yyyy-mm-dd')",RegexOptions.Multiline);
}
有些時候我偶們希望在正則表達式的替換中對替換的字符換進行簡單的處理,比如把所有的A依次替換爲B1、B2、B3……這就需在替換時對字符串進行處理,其實這個很簡單,用C#中的MatchEvaluator委託就可以了。簡單的示例如下:
引用內容:
private static int i = 0;
public static string ParseToHTML(string ubbString)
{
Regex rgx;
string htmlString = "";
MatchEvaluator me = new MatchEvaluator(AddOne);
rgx = new Regex(@"/[code/](.*?)/[//code/]");
htmlString = rgx.Replace(htmlString, me);
return htmlString;
}
public static string AddOne(Match m)
{
string code = m.Value.Substring(6, m.Value.Length - 13);
string codeString = @"<textarea name='code" + i + "' class='code_text'>" + code + "</textarea></div><br />";
i++;
return codeString;
}
public static string ParseToHTML(string ubbString)
{
Regex rgx;
string htmlString = "";
MatchEvaluator me = new MatchEvaluator(AddOne);
rgx = new Regex(@"/[code/](.*?)/[//code/]");
htmlString = rgx.Replace(htmlString, me);
return htmlString;
}
public static string AddOne(Match m)
{
string code = m.Value.Substring(6, m.Value.Length - 13);
string codeString = @"<textarea name='code" + i + "' class='code_text'>" + code + "</textarea></div><br />";
i++;
return codeString;
}