0

我正在尝试使用正则表达式来识别文本文件中的特定占位符。
每个占位符都以 % 开头和结尾。
打开 % 后的第一个单词是占位符名称,它可以具有可选属性(考虑非默认格式属性)。

一些样本是:

%MyVariable% --> 想要的输出是 Name=MyVariable , Attribute=''
%MyVariable:uppercase% --> 想要的输出是 Name=MyVariable , Attribute='uppercase'
%MyVariable:maxlen(20)% --> 想要的输出是 Name=MyVariable , Attribute='maxlen(20)'
%MyVariable:words(1,2)% --> 想要的输出是 Name=MyVariable , Attribute='words(1,2)'

请任何人帮助我建立一个正确的正则表达式来完成这项任务?

我正在使用 C# 和 .NET 框架 v4.0

谢谢

4

1 回答 1

0

以下模式将为您捕获每个占位符:

%(?<name>\w+)(?<attribute>:.*?)?%

按组名检索值:nameattribute

例子:

var matches = Regex.Matches(input, @"%(?<name>\w+)(?<attribute>:.*?)?%");
foreach (Match match in matches)
{
    var name = match.Groups["name"].Value;
    var attribute = match.Groups["attribute"].Value;
    Console.WriteLine(
        string.Format("Name={0}, Attribute='{1}'", name, attribute));
}

%说明:模式在您的占位符中以 as 开头和结尾。在内部,我们使用语法定义了两个名称组(?<group_name>some_pattern_here)。第一组用 捕获所有字母数字字符\w+,应该至少有 1 个字符。第二组捕获所有符号,直到%使用.*?. 组可能存在或不存在,因此我们将其放在?后面。就是这样。视觉上看起来像这样:

%           MyVariable                 :  words(1,2)     %
% (?<name>     \w+     ) (?<attribute> :     .*?     )?  %

请阅读MSDN 上的正则表达式参考以了解所有特殊字符。

于 2013-02-22T10:14:53.300 回答