使用正则表达式验证,以确保一个字符串包含特定的子 [英] use regex validator to ensure a string contains particular substring
问题描述
我想使用正则表达式验证,以保证一定的字符串变量包含子www.youtube.com/watch?v =
,我会怎么做此?
[RegularEx pression()]
[必需(的ErrorMessage =Youtube链接是必需)]
[StringLength(100的ErrorMessage =Youtube链接不能超过50个字符)]
公共字符串YoutubeLink {搞定;组; }
如果(YoutubeLink.Contains(www.youtube.com/watch?v=))
{
// ...
}
http://msdn.microsoft.com/en-us/library/ dy85x1sa.aspx
要使用RegularEx pression属性,必须指定正则表达式的使用方法:
[RegularEx pression(WWW \\\\。\\\\的YouTube。com /手表\\\\?V =的ErrorMessage =链接是不正确)]
公共字符串YoutubeLink {搞定;组; }
<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularex$p$pssionattribute.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularex$p$pssionattribute.aspx
我想你可能想要的是来检查任何类型的URL开始。在这种情况下,你可以使用 @^(HTTP://)???(WWW \\)的YouTube \\ .COM /手表\\ V =
的正则表达式字符串。
<一个href=\"http://regexpal.com/?flags=m®ex=%5E%28http%3A%2F%2F%29%3F%28www%5C.%29%3Fyoutube%5C.com%2Fwatch%5C%3Fv%3D&input=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D12323456%0D%0A%0D%0Ahttp%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3D12323456%0D%0A%0D%0Awww.youtube.com%2Fwatch%3Fv%3D12323456%0D%0A%0D%0Ayoutube.com%2Fwatch%3Fv%3D12323456%0D%0A%0D%0Ahttp%3A%2F%2Fwww.goingbananas.com%0D%0A%0D%0Awww.goingbananas.com%0D%0A%0D%0Ahttp%3A%2F%2Fgoingbananas.com%0D%0A%0D%0Agoingbananas.com\"相对=nofollow>见这是什么会匹配一个例子
I want to use a regex validator to ensure that a certain string variable contains the substring "www.youtube.com/watch?v="
, how would I do this?
[RegularExpression()]
[Required(ErrorMessage = "Youtube link is Required")]
[StringLength(100, ErrorMessage="Youtube link cannot exceed 50 characters")]
public string YoutubeLink { get; set; }
if (YoutubeLink.Contains("www.youtube.com/watch?v="))
{
//...
}
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
To use the RegularExpression attribute, you must specify the regex to use :
[RegularExpression("www\\.youtube\\.com/watch\\?v=", ErrorMessage = "Link is incorrect")]
public string YoutubeLink { get; set; }
I think what you might want is to check for any type of URL beginning. In that case, you would use @"^(http://)?(www\.)?youtube\.com/watch\?v="
as the regex string.
See an example of what this would match
这篇关于使用正则表达式验证,以确保一个字符串包含特定的子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!