-1
 [RegularExpression(), ErrorMessage = "Youtube link must start with www.youtube.com/watch?v=")]

I need to check if Link does NOT begin with: http://www.youtube.com/watch?v=

Alan Budzinski
  • 811
  • 3
  • 16
  • 33

3 Answers3

3

I've just created an MVC project and tested the following:

[RegularExpression("^((?!http://www.youtube.com/watch\\?v=).)*$")]

This seems to work.

More information may be found here.

If you need to check that the text does begin with a youtube link (rather than does not begin) then you can use:

[RegularExpression("http://www.youtube.com/watch\\?v=.*")]
Community
  • 1
  • 1
Dangerous
  • 4,818
  • 3
  • 33
  • 48
  • I don't know what is going on but what I want is that when my string begins with a proper youtube link the error message should NOT appear in my validation, but when I have this code (used your code) it does the opposite, the message shows when the link is proper, and when it does NOT begin like this `http://www.youtube.com/watch?v=` the error message is not showing.. – Alan Budzinski Dec 01 '11 at 23:43
  • After reading your question again I realise that the error message says that the pattern must start with the youtube link but then underneath and in the comment you say that the pattern should not begin with the youtube link which led to my confusion. – Dangerous Dec 02 '11 at 00:01
  • After long fight with this I still have not find out the correct solution, basically I want to give user an error message if the link he provided DOES NOT begin with `http://www.youtube.com/watch?v=` – Alan Budzinski Dec 02 '11 at 00:04
  • I'm not sure if its me that's confused? Should your question have been "I need to check that the link begins with: http://www.youtube.com/watch?v= instead? Regardless, I have updated my answer to reflect both possibilities. I hope this is what you wanted. – Dangerous Dec 02 '11 at 00:31
  • Could you provide more information as to why it does not work? When I tested with `http://www.youtube.com/watch?v=additionalinformation` it worked but if I tested with `http://www.google.com/watch?v=additionalinformation` then I would get an error message. Is this not what you get or is this not what you want? – Dangerous Dec 02 '11 at 10:09
  • this is what I want but it does not behave correctly, when I type in `http://www.youtube.com/watch?v=7D-eOk9EOmU` which is in the correct format the error message appears – Alan Budzinski Dec 02 '11 at 10:12
  • @AlanBudzinski You were right, that didn't work. It was missing a . character before the *. I have updated the answer. – Dangerous Dec 02 '11 at 10:38
  • Oh man, Finally it works, I'm very thankfull for your patience :) – Alan Budzinski Dec 02 '11 at 13:48
0

Try this code , Alan

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string txt="http://www.youtube.com/watch?v=";

      string re1="(http:\\/\\/www\\.youtube\\.com\\/watch\\?v=)";   
      string re2="(www\\.youtube\\.com)";

      Regex r = new Regex(re1+re2,RegexOptions.IgnoreCase|RegexOptions.Singleline);
      Match m = r.Match(txt);
      if (m.Success)
      {
            String httpurl1=m.Groups[1].ToString();
            String file1=m.Groups[2].ToString();
            Console.Write("("+httpurl1.ToString()+")"+"("+file1.ToString()+")"+"\n");
      }
      Console.ReadLine();
    }
  }
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

If you only want to check for that specific string, regex is not needed. just do something like int position = string.IndexOf("http://www.youtube.com/watch?v="); and check if position is 0

EDIT:
If you really need a regular expression you could try this: /^(?!^http:\/\/www\.youtube\.com/watch\?v=).*/

Thorbear
  • 2,303
  • 28
  • 23
  • I need the regex to use regularExpresion validator in asp.net mvc – Alan Budzinski Dec 01 '11 at 23:15
  • like mentioned in the post by @FailedDev , you need to escape the slashes. I however think that his expression does not work because it is missing the .* at the end, which makes the expression match anything at all – Thorbear Dec 01 '11 at 23:35