MVC模型属性中多行的正则表达式

Regular expression for multiple lines in MVC Model attribute

呃...我很不擅长正则表达式。

我正在尝试编写一个正则表达式来匹配 一个或多个 .NET 中的有效 Guid,以换行符分隔,用于 MVC 模型。

这是模型 属性,我现在有:

[Required]
[Editable(true)]
[DataType(DataType.MultilineText)]
[Display(Name = "Audit Result Global Ids")]
[RegularExpression(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", ErrorMessage = "Audit Result Global Ids must be a list of one or more valid GUIDs.")]
public List<Guid> AuditResultGlobalIds { get; set; }

这是我正在尝试匹配的示例..

C835EFF5-65D8-4F1B-824D-CD3FD1A04D77
A1FEFACA-6130-4E0A-BAC2-F101C2F4554C
ACD3D3FC-E841-45E9-A081-C113231E56EF

我有一个很好的单一 RegEx 模式:

@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$"

我只需要多行部分。


更新: 这是我最终采用的确切解决方案。

自定义验证class:

using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;

/// <summary>
/// The regular expression on list elements.
/// </summary>
public class RegularExpressionOnListElements : RegularExpressionAttribute
{
    /// <summary>
    /// Initializes a new instance of the <see cref="RegularExpressionOnListElements"/> class.
    /// </summary>
    /// <param name="pattern">
    /// The pattern.
    /// </param>
    public RegularExpressionOnListElements(string pattern)
        : base(pattern)
    {
    }

    #region Public Methods and Operators

    /// <summary>
    /// The is valid.
    /// </summary>
    /// <param name="value">
    /// The value.
    /// </param>
    /// <param name="validationContext">
    /// the validationContext
    /// </param>
    /// <returns>
    /// The <see cref="ValidationResult "/>.
    /// </returns>
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            string[] lines = Regex.Split(value.ToString(), "\r\n");

            if (lines.Length > 0)
            {
                if (!lines.All(elem => base.IsValid(elem)))
                {
                    return new ValidationResult(
                        "Audit Result Global Ids must be a list of one or more valid GUIDs.");
                }
            }
        }

        return ValidationResult.Success;
    }

    #endregion
}

型号属性:

/// <summary>
///     Gets or sets the list of audit result global ids.
/// </summary>
[Editable(true)]
[DataType(DataType.MultilineText)]
[Display(Name = "Audit Result Global Ids")]
[RegularExpressionOnListElements(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")]
public string AuditResultGlobalIds { get; set; }

您可以制作自定义验证属性。

RegularExpressionOnList

public class RegularExpressionOnListElements : RegularExpressionAttribute
{
    public RegularExpressionOnListElements(string pattern)
        : base(pattern)
    {
    }

    public override bool IsValid(object value)
    {
        var list = value as IList;
        if (list != null)
        {
            return list.All(elem => base.IsValid(elem));
        }
        return true;
    }
}

用法:

[Required]
[Editable(true)]
[DataType(DataType.MultilineText)]
[Display(Name = "Audit Result Global Ids")]
[RegularExpressionOnListElements(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", ErrorMessage = "Audit Result Global Ids must be a list of one or more valid GUIDs.")]
public List<Guid> AuditResultGlobalIds { get; set; }

注意:代码尚未经过测试。