我可以在格式的文本区域中有 0 个或多个子字符串{key-value}Some text{/key}
,
例如This is my {link-123}test{/link} text area
我想遍历与此模式匹配的任何项目,根据键和值执行和操作,然后用新字符串替换此子字符串(由基于键的操作检索的锚链接)。
我将如何在 C# 中实现这一点?
如果这些标签没有嵌套,那么您只需要遍历文件一次;如果嵌套是可能的,那么您需要对每一层嵌套进行一次迭代。
此答案假定大括号仅作为标记分隔符出现(而不是,例如,内部注释):
result = Regex.Replace(subject,
@"\{ # opening brace
(?<key>\w+) # Match the key (alnum), capture into the group 'key'
- # dash
(?<value>\w+) # Match the value (alnum), capture it as above
\} # closing brace
(?<content> # Match and capture into the group 'content':
(?: # Match...
(?!\{/?\k<key>) # (unless there's an opening or closing tag
. # of the same name right here) any character
)* # any number of times
) # End of capturing group
\{/\k<key>\} # Match the closing tag.",
new MatchEvaluator(ComputeReplacement), RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
public String ComputeReplacement(Match m) {
// You can vary the replacement text for each match on-the-fly
// m.Groups["key"].Value will contain the key
// m.Groups["value"].Value will contain the value of the match
// m.Groups["value"].Value will contain the content between the tags
return ""; // change this to return the string you generated here
}
像这样的东西?
Regex.Replace(text,
"[{](?<key>[^-]+)-(?<value>[^}])[}](?<content>.*?)[{][/]\k<key>[}]",
match => {
var key = match.Groups["key"].Value;
var value= match.Groups["value"].Value;
var content = match.Groups["content"].Value;
return string.format("The content of {0}-{1} is {2}", key, value, content);
});
使用 .net 正则表达式库。下面是一个使用 Matches 方法的示例:
http://www.dotnetperls.com/regex-matches
对于替换文本,请考虑使用 Antlr 等模板引擎
http://www.antlr.org/wiki/display/ANTLR3/Antlr+3+CSharp+Target
以下是 Matches 博客中的示例
使用系统;使用 System.Text.RegularExpressions;
class Program
{
static void Main()
{
// Input string.
const string value = @"said shed see spear spread super";
// Get a collection of matches.
MatchCollection matches = Regex.Matches(value, @"s\w+d");
// Use foreach loop.
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine("Index={0}, Value={1}", capture.Index, capture.Value);
}
}
}
}
有关 C# 正则表达式语法的更多信息,您可以使用此备忘单:
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet