首页 > 解决方案 > 使用正则表达式对字符串进行分组

问题描述

我有以下字符串

validates="required positiveInteger" label="Enter the Total Value." name="totalvalue" visibleif="hasvalue:Yes"

所以字符串有 4 个属性validates, label,namevisibleif各自的值

我正在使用dotliquid 的正则表达式实用程序类对属性进行分组。在initialize方法下面的markup参数值是上面的字符串。

public class TextBox : DotLiquid.Tag
{
    //R.B and R.Q are dotliquid's utility method
    private static readonly Regex Syntax = R.B(R.Q(@"(?<validation>{0}+)(\s(?<label>{0}+))?(\s(?<name>{0}+))?(\s(?<onlyif>{0}+))?"), Liquid.QuotedFragment);

    private string[] _validations;
    private string[] _label;
    private string[] _name;
    private string[] _onlyif;

    public override void Initialize(string tagName, string markup, List<string> tokens)
    {
        var syntaxMatch = Syntax.Match(markup);
        if (syntaxMatch.Success)
        {
            _validations = syntaxMatch.Groups["validation"].Value.Split("=").TrimQuotes().ToArray();
            _label = syntaxMatch.Groups["label"].Value.Split("=").TrimQuotes().ToArray();
            _name = syntaxMatch.Groups["name"].Value.Split("=").TrimQuotes().ToArray();
            _onlyif = syntaxMatch.Groups["onlyif"].Value.Split("=").TrimQuotes().ToArray();
        }
        else
        {
            throw new SyntaxException("Invalid syntax");
        }

        base.Initialize(tagName, markup, tokens);
    }

    public override void Render(Context context, TextWriter result)
    {
        base.Render(context, result);
    }
}

该代码仅使用给定的字符串,但是存在问题:
1>如果属性的顺序不同,则分组会分配错误的值。

2>namelabel属性是必需的,但validatesvisibleif属性不是必需的。RegEx 应该验证这一点。

3>如果标记有任何其他额外属性,RegEx 必须失败。

有人可以帮助正确的正则表达式吗?

标签: c#.netregexdotliquid

解决方案


我怀疑这是否是我们想要解决的问题。但是,在我看来,我们想要捕获属性值。如果是这种情况,我们可能希望从一个简单的表达式开始,然后根据需要进行修改和更改。例如,我们可以使用:

(validates|label|name|visibleif)=("(.+?)")\s?

如果我们希望其他字符串失败,那可能很简单,但是我不太确定其他可能的和期望的字符串来提出任何建议。

在此处输入图像描述

正则表达式

如果不需要此表达式,可以在regex101.com中对其进行修改或更改。

正则表达式电路

jex.im可视化正则表达式:

在此处输入图像描述

示例测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(validates|label|name|visibleif)=(""(.+?)"")\s?";
        string input = @"validates=""required positiveInteger"" label=""Enter the Total Value."" name=""totalvalue"" visibleif=""hasvalue:Yes""
validates=""required positiveInteger"" label=""Enter the Total Value."" name=""totalvalue"" visibleif=""hasvalue:Yes"" fail_attribute=""Undesired""";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

演示

const regex = /(validates|label|name|visibleif)=("(.+?)")\s?/gm;
const str = `validates="required positiveInteger" label="Enter the Total Value." name="totalvalue" visibleif="hasvalue:Yes"
validates="required positiveInteger" label="Enter the Total Value." name="totalvalue" visibleif="hasvalue:Yes" fail_attribute="Undesired"`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

演示


推荐阅读