本文整理匯總了C#中System.Text.RegularExpressions.Regex.ReplaceGroup方法的典型用法代碼示例。如果您正苦於以下問題:C# Regex.ReplaceGroup方法的具體用法?C# Regex.ReplaceGroup怎麽用?C# Regex.ReplaceGroup使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.ReplaceGroup方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ColorizeToken
private List<CrayonString.CrayonToken> ColorizeToken(CrayonString.CrayonToken token, Regex regex) {
if (token.Color != CrayonColor.Default) {
// do not process colorized tokens
return new List<CrayonString.CrayonToken>() { token };
}
var result = new StringBuilder();
var text = token.Text;
var matches = regex.Matches(text);
var idx = 0;
if (matches.Count > 0)
{
var names = regex.GetGroupNames();
foreach (Match m in matches)
{
var replaced = m.Captures[0].Value;
foreach (var name in names)
{
var i = 0;
/// ignore groups without names
if (int.TryParse(name, out i)) continue;
var group = m.Groups[name];
var color = GetColor(name);
var coloredString = color.Format(group.Value);
replaced = regex.ReplaceGroup(replaced, name, coloredString);
}
var before = text.Substring(idx, m.Index - idx);
result.Append(before);
result.Append(replaced);
idx = m.Index + m.Length;
}
}
result.Append(text.Substring(idx, text.Length - idx));
return new CrayonString(result.ToString()).Tokenize();
}
示例2: ColorizeString
private CrayonString ColorizeString(CrayonString str, Regex regex)
{
var result = new StringBuilder();
var matches = regex.Matches(str.Text);
var idx = 0;
if (matches.Count > 0)
{
var names = regex.GetGroupNames();
foreach (Match m in matches)
{
var replaced = m.Captures[0].Value;
foreach (var name in names)
{
var i = 0;
/// ignore groups without names
if (int.TryParse(name, out i)) continue;
var group = m.Groups[name];
var color = GetColor(name);
var coloredString = color.Format(group.Value);
replaced = regex.ReplaceGroup(replaced, name, coloredString);
}
var before = str.Text.Substring(idx, m.Index - idx);
result.Append(before);
result.Append(replaced);
idx = m.Index + m.Length;
}
}
result.Append(str.Text.Substring(idx, str.Text.Length - idx));
return new CrayonString(result.ToString());
}