MVC模型属性中的多行正则表达式
本文关键字:正则表达式 模型 属性 MVC | 更新日期: 2023-09-27 18:06:23
…我不擅长正则表达式。
我试图写一个正则表达式,将匹配一个或多个有效Guids在。net中,由新行字符分隔,在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}$"
我只需要多行部分。
UPDATE:这是我最终使用的确切解决方案。
自定义验证类:
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; }
注:代码未经过测试