0

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; }
ruakh
  • 175,680
  • 26
  • 273
  • 307
Alan Budzinski
  • 811
  • 3
  • 16
  • 33
  • what language do you want the answer for? Also, there are plenty of resources out there that show how to use regex to do this – Boundless Dec 01 '11 at 21:42
  • 1
    From your code it looks like we are talking about c# (correct me if I'm wrong (and maybe add an apropreiate tag). In this case: Why would you need a regex for this? `YoutubeLink.Contains("www.youtube.com/watch?v=")` will return whether the substring is contained in `YoutubeLink` or not. If I was wrong about the language I would still guess that such a method would exist – Grizzly Dec 01 '11 at 21:45

2 Answers2

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

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularexpressionattribute.aspx


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

Bruno
  • 4,685
  • 7
  • 54
  • 105
  • I would like to use the [RegularExpression()] validator though – Alan Budzinski Dec 01 '11 at 21:46
  • 1
    @ibiza: You *should* quote the `.`s, and you **must** quote the `?`: `@"www\.youtube\.com/watch\?v="`. – ruakh Dec 01 '11 at 21:54
  • sorry I ment to check if the string starts with www.youtube.com/watch?v= not if it is the same – Alan Budzinski Dec 01 '11 at 21:59
  • a regular expression will find a match if the regex is found within a string...it has not to be the same as the whole string. If you want to check if it *starts* with www. and so on, you would need to add a caret (^) at the beginning of the regex, but then it wouldn't return a match for an url starting with http://, are you sure you want that the link absolutely need to begin with www? – Bruno Dec 01 '11 at 22:04
  • the bottom line is, I need to check whether my string begins with "http://www.youtube.com/watch?v=" without the quotation marks p.s. stackoverflow removes the h t t p:// turnes it into a link – Alan Budzinski Dec 01 '11 at 22:14
  • 1
    formulated as is ( I need ... BEGINS with), then use `^http://youtube\.com/watch\?v=` for the regex. But this won't match URLs using www. in it. – Bruno Dec 01 '11 at 22:15
  • 1
    @AlanBudzinski: Your string is getting interpreted by the StackOverflow software as a link, and therefore processed in such a way that we can't see exactly what it looks like. You need to wrap it in the backquote character, `, to tell the software to treat your string as source-code. – ruakh Dec 01 '11 at 22:16
  • `[RegularExpression("^(http://)?(www\\.)?youtube\\.com/watch\\?v=", ErrorMessage = "Youtube link must start with http://www.youtube.com/watch?v=")]` this is my code, it only works when my string is exactly like this: `http://www.youtube.com/watch?v=` but when I have additional characters after the ?v= it gives me validation error/message – Alan Budzinski Dec 01 '11 at 22:38
-1

Maybe much easier to use something like (Python):

ytlink  = 'www.youtube.com/watch?v='
if(ytlink in yourfrase):
  doYourLogic()
else:
  smthElse()

Please correct me if I understood your question wrong.

smottt
  • 3,272
  • 11
  • 37
  • 44
Ishikawa Yoshi
  • 1,779
  • 8
  • 22
  • 43