Regex不是用c#工作的,而是用javascript工作的
我试图验证字符串不应该以某个字符开头,也不应该包含Regex不是用c#工作的,而是用javascript工作的,javascript,c#,regex,model-view-controller,Javascript,C#,Regex,Model View Controller,我试图验证字符串不应该以某个字符开头,也不应该包含 [必需] [显示(Name=“First Name”)] [MaxLength(50)] [RegularExpression(@)(?=(^(?).*[>根据您对正则表达式需要执行的操作的描述,以下模式应该可以工作: ^(?![@=*-])(?!.*[<>]).*$ ^(?![@=*-])(?!.*[])*$ 说明: ^ (?![@=*-]) from the start of string, assert that @
[必需]
[显示(Name=“First Name”)]
[MaxLength(50)]
[RegularExpression(@)(?=(^(?).*[>根据您对正则表达式需要执行的操作的描述,以下模式应该可以工作:
^(?![@=*-])(?!.*[<>]).*$
^(?![@=*-])(?!.*[])*$
说明:
^
(?![@=*-]) from the start of string, assert that @=*- is not the first character
(?!.*[<>]) also assert that <> does not appear anywhere in the string
.* then match anything (our assertions having be proven true)
$
^
(?![@=*-])从字符串的开头,断言@=*-不是第一个字符
(?!.*[])还断言字符串中的任何位置都不会出现
*然后匹配任何内容(我们的断言已被证明是正确的)
$
这个模式也适用于C#,您可以在下面的第二个演示中看到这一点
根据您对正则表达式需要执行的操作的描述,以下模式应该可以工作:
^(?![@=*-])(?!.*[<>]).*$
^(?![@=*-])(?!.*[])*$
说明:
^
(?![@=*-]) from the start of string, assert that @=*- is not the first character
(?!.*[<>]) also assert that <> does not appear anywhere in the string
.* then match anything (our assertions having be proven true)
$
^
(?![@=*-])从字符串的开头,断言@=*-不是第一个字符
(?!.*[])还断言字符串中的任何位置都不会出现
*然后匹配任何内容(我们的断言已被证明是正确的)
$
这个模式也适用于C#,您可以在下面的第二个演示中看到这一点
有了合适的集合验证器,它也可以在C#中工作
公共类名称prop
{
字符串m_firstname
[Required]
[Display(Name = "First name")]
[MaxLength(50)]
[RegularExpression(@"^(?![@=*-])(?!.*[<>]).*$", ErrorMessage = "firstname contains <> or start with @=*-")]
public string firstname
{
get { return m_firstname; }
set
{
Validator.ValidateProperty(value,
new ValidationContext(this, null, null) { MemberName = "firstname" });
m_firstname = value;
}
}
[必需]
[显示(Name=“First Name”)]
[MaxLength(50)]
[RegularExpression(@“^(?![@=*-])(?!.[]).*$”,ErrorMessage=“firstname包含或以@=*-”开头]
公共字符串名
{
获取{return m_firstname;}
设置
{
Validator.ValidateProperty(值,
新的ValidationContext(this,null,null){MemberName=“firstname”});
m_firstname=值;
}
}
}
公共课程
{
static void Main(字符串[]args)
{
NameProp np=新的NameProp();
尝试
{
np.firstname=“JJ”;
}
捕获(ValidationException ex)
{
控制台写入线(例如消息);
}
}
}
有了合适的集合验证器,它也可以在C#中工作
公共类名称prop
{
字符串m_firstname
[Required]
[Display(Name = "First name")]
[MaxLength(50)]
[RegularExpression(@"^(?![@=*-])(?!.*[<>]).*$", ErrorMessage = "firstname contains <> or start with @=*-")]
public string firstname
{
get { return m_firstname; }
set
{
Validator.ValidateProperty(value,
new ValidationContext(this, null, null) { MemberName = "firstname" });
m_firstname = value;
}
}
[必需]
[显示(Name=“First Name”)]
[MaxLength(50)]
[RegularExpression(@“^(?![@=*-])(?!.[]).*$”,ErrorMessage=“firstname包含或以@=*-”开头]
公共字符串名
{
获取{return m_firstname;}
设置
{
Validator.ValidateProperty(值,
新的ValidationContext(this,null,null){MemberName=“firstname”});
m_firstname=值;
}
}
}
公共课程
{
static void Main(字符串[]args)
{
NameProp np=新的NameProp();
尝试
{
np.firstname=“JJ”;
}
捕获(ValidationException ex)
{
控制台写入线(例如消息);
}
}
}
这个正则表达式是什么?从表达式中间的$$中可以看到什么?请给出这个正则表达式应该做些什么的背景。甚至可以在两个地方使用一个更简单的模式。@ AlexSeleznyov,你可以用它来帮助你理解我正在做的事情。@ TimiGeEeleSee不能包含<或>D不能从@,-,=,*字符开始,请检查一下这个答案,它可能会帮助。这个正则表达式是什么?从表达式中的$$中,你期望什么?请给出一些关于这个正则表达式应该做什么的背景。甚至可以在两个地方使用更简单的模式。@ AlexSeleznyov,你可以用它来帮助你。为了理解我在做什么。@TimBiegeleisen不能包含<或>,不能以@、、=、*字符开头,请检查一下这个答案,它可能会有帮助。谢谢,我正在用我的c代码测试它。给我5分钟。它在用javascript,但不在用c。你在用javascript正则表达式引擎测试它。在c中,我在用RegularExpression属性。@pravaljain该模式在C#中对我来说似乎很好。请看一下我添加到我的答案中的演示。我猜你使用了他们的一个API不正确。谢谢你,Tim,但是对于你的演示,我用我的正则表达式替换了你的正则表达式,我的也在工作,但我不知道为什么它在属性上不起作用。我想我在中使用了API正确。谢谢,我正在我的c代码上测试它。给我5分钟。它在javascript上工作,但在c上不工作。你正在javascript正则表达式引擎上测试它。在c中,我正在使用RegularExpression属性。@pravaljain该模式在c中似乎对我很好。看看我添加到答案中的演示。我猜你正在使用谢谢,Tim,但是为了你的演示,我用我的正则表达式替换了你的正则表达式,我的正则表达式也在工作,但我不知道为什么它在属性上不工作。我想我使用的API不正确。另外,请参阅以获取更多详细信息。因此,请参阅以获取更多详细信息