本文整理汇总了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());
}