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

  • Thread starter Thread starter Phillip Vong
  • Start date Start date
P

Phillip Vong

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)\$
 
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 said:
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
 
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 said:
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, 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 said:
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.
 
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!
*************************************************
 
Back
Top