Regex 使用正则表达式时总是失败

Regex 使用正则表达式时总是失败,regex,asp.net-mvc,data-annotations,Regex,Asp.net Mvc,Data Annotations,我想把邮件限制在那些来自我公司的邮件上。这就是我所拥有的,但它总是失败: [Required] [EmailAddress] [Display(Name = "Email")] [RegularExpression(@"/\w+@mycompany\.com/", ErrorMessage = "You must use your mycompany email to register")] public string Email { get; set; } 此电子邮件始终返回错误:coope

我想把邮件限制在那些来自我公司的邮件上。这就是我所拥有的,但它总是失败:

[Required]
[EmailAddress]
[Display(Name = "Email")]
[RegularExpression(@"/\w+@mycompany\.com/", ErrorMessage = "You must use your mycompany email to register")]
public string Email { get; set; }
此电子邮件始终返回错误:
cooper@mycompany.com
。我做错了什么?

在C#regex中,与PHP、JavaScript和其他一些语言不同,您不必使用分隔符

[RegularExpression(@"\w+@mycompany\.com", ErrorMessage = "You must use your mycompany email to register")]
正则表达式锚定在
RegularExpressionAttribute
中,它将匹配

  • \w+
    -以字母数字开头,出现一次或多次
  • @
    -然后有一个文本
    @
  • mycompany\.com
    -以文字
    mycompany.com
    结尾(必须转义点才能匹配文字点)

@“\w+@mycompany\.com”
删除
/
Prolly want锚定符,以及在其他语言中使用前斜杠来分隔正则表达式字符串,即代替单引号或双引号。