【问题标题】:Configure text output based on pattern根据模式配置文本输出
【发布时间】:2021-09-30 13:31:31
【问题描述】:

我正在开发一个基于用户创建的配置文件读取和写入文件的应用程序。作为其中的一部分,我想输出用户模式提供的阅读文本,但我想不出最好的方法。

例子:

配置中的输出模式:

{Name} version: {1}
{Name}_{1}

同时: 名称 - 将需要替换为字符串变量值

1,2,3... - 需要替换为对应的正则表达式匹配组值。

Regex.Replace 真的不适合我。像{Name} 这样的属性会很好,但我无法将数字转换为正则表达式匹配组。

我的第二个想法是将模式与 {} 值匹配并使用 switch case 替换它们,但这看起来不是最好的想法:

Regex a = new Regex("{(.*)}");

foreach (Match m in a.Matches(Pattern))
{
    switch (m.Groups[1].Value)
    {
        case "1":
            {                            
                return MatchedGroups[1].Value;
            }
        case "2":
            {                            
                return MatchedGroups[2].Value;
            }
        case "Name":
            {
                return Name;
            }
    }
}

编辑 1:

更好地说明它的示例,我拥有什么以及我想要获得什么:

我有:

String InputText = "12/03/2015 *** [RandomText] // 1.04.1112V";
String regex = @"([0-9/]*) \*\*\* \[([A-Za-z]*)\] \/\/ (.*)";
  // #group 1 = 12/03/2015
  // #group 2 = RandomText
  // #group 3 = 1.04.1112V
String Name = "GoogleChrome";
String OutputPattern1 = "{1} - {Name} version {3}";
String OutputPattern2 = "{Name}_{3}";

并且有上面我想要输出的变量和模式:

#1 : 12/03/2015 - GoogleChrome version 1.04.1112V

#2 : GoogleChrome_1.04.1112V

输出模式将由用户创建,所以我无法预测。

【问题讨论】:

  • 听起来好像你想要Regex.Replace(a, @"{(?:(\d+)|Name)}", x => x.Groups[1].Success ? x.Groups[1].Value : Name)
  • 我不确定我们是否完全了解对方。我可能有多个匹配组,用户提供的输出模式可能会提及 {1} 或 {2} 或 {5} 或任何其他将引用 Match.Groups[digit].Value 的数字。
  • 我来说明一下:你想得到ideone.com/Gi7kXX吗?
  • @WiktorStribiżew 不完全是,我编辑了我的帖子看看。

标签: c# regex text output config


【解决方案1】:

你可以使用

string InputText = "12/03/2015 *** [RandomText] // 1.04.1112V";
string regex = @"([0-9/]*) \*\*\* \[([A-Za-z]*)\] \/\/ (.*)";
string Name = "GoogleChrome";
string OutputPattern1 = "{1} - {Name} version {3}";
string OutputPattern2 = "{Name}_{3}";
string rx =  @"{(?:(\d+)|Name)}";
var match  = Regex.Match(InputText, regex);
if (match.Success) {
    Console.WriteLine(
        Regex.Replace(OutputPattern1, rx, x=>
            x.Groups[1].Success? match.Groups[int.Parse(x.Groups[1].Value)].Value : Name)
    );
    Console.WriteLine(
        Regex.Replace(OutputPattern2, rx, x=>
            x.Groups[1].Success? match.Groups[int.Parse(x.Groups[1].Value)].Value : Name)
    );
}
// => 12/03/2015 - GoogleChrome version 1.04.1112V
//    GoogleChrome_1.04.1112V

请参阅C# demo

{(?:(\d+)|Name)} 正则表达式匹配

  • { - 一个 { 字符
  • (?:(\d+)|Name) - 一位或多位数字(捕获到第 1 组)或 Name
  • } - } 字符。

如果组 1 匹配,则使用整体匹配中的对应组替换 OutputPatternX 中的 {x} 子字符串,否则使用 Name 变量文本。

如果您需要在替换之前检查组 ID 是否存在*(例如,您有 {4} 而您的正则表达式只有 3 个组),您需要

Regex.Replace(OutputPattern1, rx, x=>
    !x.Groups[1].Success ? Name:
        int.Parse(x.Groups[1].Value) < match.Groups.Count ? 
            match.Groups[int.Parse(x.Groups[1].Value)].Value :
            x.Value)

this C# demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多