Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Simple Regex to Require start with http:// or https://

Reply
Thread Tools

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

 
 
Phillip Vong
Guest
Posts: n/a
 
      10-16-2006
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|g ov)\$


 
Reply With Quote
 
 
 
 
Mark E. Hansen
Guest
Posts: n/a
 
      10-16-2006
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|g ov)\$
>
>


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.
 
Reply With Quote
 
 
 
 
apathetic
Guest
Posts: n/a
 
      10-16-2006
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

 
Reply With Quote
 
Phillip Vong
Guest
Posts: n/a
 
      10-16-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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|g ov)\$
>>
>>

>
> 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.



 
Reply With Quote
 
Mark E. Hansen
Guest
Posts: n/a
 
      10-16-2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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|g ov)\$
>>>
>>>

>>
>> 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.

>
>

 
Reply With Quote
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      10-17-2006
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:(E-Mail Removed)...
> 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|g ov)\$
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Multiple "require" calls -> app slow to start Philippe Lang Ruby 2 02-07-2007 05:33 PM
do some cisco switches require "copy run start" sqrfolkdnc Cisco 7 09-14-2005 09:05 AM
New to Java Programming- Require Good IDE to start rahul8143@gmail.com Java 3 08-11-2005 08:07 PM
Simple use-vs-require problem clarence@silcom.com Perl Misc 2 01-25-2005 09:12 PM



Advertisments