【问题标题】:Regular expression for multiple lines in MVC Model attributeMVC模型属性中多行的正则表达式
【发布时间】:2015-08-03 23:43:20
【问题描述】:

呃……我不擅长正则表达式。

我正在尝试编写一个正则表达式,它将匹配 .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}$"

我只需要多行部分。


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

自定义验证类:

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; }

【问题讨论】:

  • 为什么在这里使用正则表达式?为什么不尝试使用Guid.TryParse() 解析每一行?

标签: c# regex asp.net-mvc modelattribute multilinestring


【解决方案1】:

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

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; }

注意:代码未经测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多