#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Simple Regex Help


    I'm terrible with Regex, and I just need a very simple regex for a C# validator. It's basically a YouTube regex, however all the youtube regex's I've found in google searches actually have too many rules for what my client wants. Here's all I need to follow.

    the URL must start with any of the following, potentially with http:// in front

    www.youtube
    youtube
    youtu.be


    that's it! lol. I admit I'm terrible with Regex's. I've tried doing this myself and been unsuccessful and it's at the point that this is taking too much time. Would love some help with this.

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,918
    Rep Power
    1045
    Hi,

    not sure why you think you need a regex for this.

    Fetch the domain from the URL (I guess the Uri class does this) and check if it starts with one of those three strings. That's it.

    A regex is completely useless in this case, because you have constant strings rather than a pattern.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    well, I have several fields that are using different asp validators and are all part of a ValidationGroup so that all the error messages appear in the same alert.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,918
    Rep Power
    1045
    So you must use a single regex to validate the URL?

    Code:
    ^(?:https?://)?(?:www\.youtube|youtu\.?be)
    I guess you have to double all backslashes in C#. Plus you need to make the regex case-insensitive, because domains are as well.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo