Simple Regex to Require start with http:// or https://

Discussion in 'ASP .Net' started by Phillip Vong, Oct 16, 2006.

  1. Phillip Vong

    Phillip Vong Guest

    Can someone help me write a simple regex that require a textbox start with
    http:// or https:// ?

    I don't know how to write regex. I usually rely on http://regexlib.com but
    I couldn't find anything good. The closest one I found was this one, but it
    requires a www also and not all my sites have www. Thanks in advance.

    ^(ht|f)tp((?<=http)s)?://((?<=http://)www|(?<=https://)www|(?<=ftp://)ftp)\.(([a-z][0-9])|([0-9][a-z])|([a-z0-9][a-z0-9\-]{1,2}[a-z0-9])|([a-z0-9][a-z0-9\-](([a-z0-9\-][a-z0-9])|([a-z0-9][a-z0-9\-]))[a-z0-9\-]*[a-z0-9]))\.(co|me|org|ltd|plc|net|sch|ac|mod|nhs|police|gov)\$
     
    Phillip Vong, Oct 16, 2006
    #1
    1. Advertising

  2. On 10/16/06 14:16, Phillip Vong wrote:
    > Can someone help me write a simple regex that require a textbox start with
    > http:// or https:// ?
    >
    > I don't know how to write regex. I usually rely on http://regexlib.com but
    > I couldn't find anything good. The closest one I found was this one, but it
    > requires a www also and not all my sites have www. Thanks in advance.
    >
    > ^(ht|f)tp((?<=http)s)?://((?<=http://)www|(?<=https://)www|(?<=ftp://)ftp)\.(([a-z][0-9])|([0-9][a-z])|([a-z0-9][a-z0-9\-]{1,2}[a-z0-9])|([a-z0-9][a-z0-9\-](([a-z0-9\-][a-z0-9])|([a-z0-9][a-z0-9\-]))[a-z0-9\-]*[a-z0-9]))\.(co|me|org|ltd|plc|net|sch|ac|mod|nhs|police|gov)\$
    >
    >


    Well, if that string requires that the text begin with what you want, followed
    by other stuff, can't you just remove the other stuff?

    If you look at the beginning of that string:

    ^(ht|f)tp((?<=http)s)?://

    That says that the value must be one of the following:

    http://
    https://
    ftp://

    Isn't that what you want? If you really don't want the "ftp://", you can
    change it to remove the ftp junk, but I would just use it the way it is.
     
    Mark E. Hansen, Oct 16, 2006
    #2
    1. Advertising

  3. Phillip Vong

    apathetic Guest

    Phillip Vong wrote:
    > Can someone help me write a simple regex that require a textbox start with
    > http:// or https://


    All you need is:

    ^https?://

    Where the special characters are:

    ^ = start of string
    ? = optional character

    Regards,
    apathetic
     
    apathetic, Oct 16, 2006
    #3
  4. Phillip Vong

    Phillip Vong Guest

    Mark, thanks for the quick reply. I stuck
    ^(ht|f)tp((?<=http)s)?://

    In the Regular Expression validator and it didn't work. Even if I start the
    text box with http:// or https://, it still triggers the validator and I get
    the error message.




    "Mark E. Hansen" <> wrote in message
    news:...
    > On 10/16/06 14:16, Phillip Vong wrote:
    >> Can someone help me write a simple regex that require a textbox start
    >> with
    >> http:// or https:// ?
    >>
    >> I don't know how to write regex. I usually rely on http://regexlib.com
    >> but
    >> I couldn't find anything good. The closest one I found was this one, but
    >> it
    >> requires a www also and not all my sites have www. Thanks in advance.
    >>
    >> ^(ht|f)tp((?<=http)s)?://((?<=http://)www|(?<=https://)www|(?<=ftp://)ftp)\.(([a-z][0-9])|([0-9][a-z])|([a-z0-9][a-z0-9\-]{1,2}[a-z0-9])|([a-z0-9][a-z0-9\-](([a-z0-9\-][a-z0-9])|([a-z0-9][a-z0-9\-]))[a-z0-9\-]*[a-z0-9]))\.(co|me|org|ltd|plc|net|sch|ac|mod|nhs|police|gov)\$
    >>
    >>

    >
    > Well, if that string requires that the text begin with what you want,
    > followed
    > by other stuff, can't you just remove the other stuff?
    >
    > If you look at the beginning of that string:
    >
    > ^(ht|f)tp((?<=http)s)?://
    >
    > That says that the value must be one of the following:
    >
    > http://
    > https://
    > ftp://
    >
    > Isn't that what you want? If you really don't want the "ftp://", you can
    > change it to remove the ftp junk, but I would just use it the way it is.
     
    Phillip Vong, Oct 16, 2006
    #4
  5. On 10/16/06 14:49, Phillip Vong wrote:
    > Mark, thanks for the quick reply. I stuck
    > ^(ht|f)tp((?<=http)s)?://
    >
    > In the Regular Expression validator and it didn't work. Even if I start the
    > text box with http:// or https://, it still triggers the validator and I get
    > the error message.


    But using the longer expression works?

    I don't understand, then. Sorry.

    >
    >
    >
    >
    > "Mark E. Hansen" <> wrote in message
    > news:...
    >> On 10/16/06 14:16, Phillip Vong wrote:
    >>> Can someone help me write a simple regex that require a textbox start
    >>> with
    >>> http:// or https:// ?
    >>>
    >>> I don't know how to write regex. I usually rely on http://regexlib.com
    >>> but
    >>> I couldn't find anything good. The closest one I found was this one, but
    >>> it
    >>> requires a www also and not all my sites have www. Thanks in advance.
    >>>
    >>> ^(ht|f)tp((?<=http)s)?://((?<=http://)www|(?<=https://)www|(?<=ftp://)ftp)\.(([a-z][0-9])|([0-9][a-z])|([a-z0-9][a-z0-9\-]{1,2}[a-z0-9])|([a-z0-9][a-z0-9\-](([a-z0-9\-][a-z0-9])|([a-z0-9][a-z0-9\-]))[a-z0-9\-]*[a-z0-9]))\.(co|me|org|ltd|plc|net|sch|ac|mod|nhs|police|gov)\$
    >>>
    >>>

    >>
    >> Well, if that string requires that the text begin with what you want,
    >> followed
    >> by other stuff, can't you just remove the other stuff?
    >>
    >> If you look at the beginning of that string:
    >>
    >> ^(ht|f)tp((?<=http)s)?://
    >>
    >> That says that the value must be one of the following:
    >>
    >> http://
    >> https://
    >> ftp://
    >>
    >> Isn't that what you want? If you really don't want the "ftp://", you can
    >> change it to remove the ftp junk, but I would just use it the way it is.

    >
    >
     
    Mark E. Hansen, Oct 16, 2006
    #5
  6. You can option out the www and make it domain name straight or some other
    word. But, that would open things up to where you might as well do something
    like replace the wwww with

    \w{1,5}

    or somehting similar (alpha between 1 and 5 chars long). You can also do
    something like \w+ etc. to state you do not know how long.


    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com

    *************************************************
    Think outside of the box!
    *************************************************
    "Phillip Vong" <phillip_vong*at*yahoo*dot*com> wrote in message
    news:...
    > Can someone help me write a simple regex that require a textbox start with
    > http:// or https:// ?
    >
    > I don't know how to write regex. I usually rely on http://regexlib.com
    > but I couldn't find anything good. The closest one I found was this one,
    > but it requires a www also and not all my sites have www. Thanks in
    > advance.
    >
    > ^(ht|f)tp((?<=http)s)?://((?<=http://)www|(?<=https://)www|(?<=ftp://)ftp)\.(([a-z][0-9])|([0-9][a-z])|([a-z0-9][a-z0-9\-]{1,2}[a-z0-9])|([a-z0-9][a-z0-9\-](([a-z0-9\-][a-z0-9])|([a-z0-9][a-z0-9\-]))[a-z0-9\-]*[a-z0-9]))\.(co|me|org|ltd|plc|net|sch|ac|mod|nhs|police|gov)\$
    >
     
    Cowboy \(Gregory A. Beamer\), Oct 17, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    768
    Reedick, Andrew
    Jul 1, 2008
  2. Axel
    Replies:
    8
    Views:
    1,100
    Adrienne Boswell
    Apr 27, 2009
  3. jotto
    Replies:
    4
    Views:
    392
    jotto
    Oct 2, 2006
  4. Naveen Dhanuka
    Replies:
    1
    Views:
    286
  5. Wladimir Borsov
    Replies:
    4
    Views:
    304
    Tintin
    May 11, 2006
Loading...

Share This Page