Solved

regexp - must contain "http://"

Posted on 2011-03-14
2
265 Views
Last Modified: 2012-05-11
Hi,
I want to validate url using regular expression.
I have found this code:
^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$

Open in new window

but it also validates urls with ftp or https. I just need to change it so http://domain.com or http://sub.domain.com can only be possible.

^http://\\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$
is that correct?
0
Comment
Question by:static86
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 35134229
To disallow ftp and https:

^http:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35137545
__NO POINTS__

As a clarification, the "amp" inside of the square brackets is really redundant since each of those characters is examined individually (not wholly as "amp") and you already have an alphabetic range at the beginning of the class. You can safely remove the "amp" and leave "&;" an not effect the results of the pattern. Also, none of those characters (except dash and backslash) which are escaped ( \ ) inside of the character class need to be escaped (since they are inside of a character class ( [ ... ] )). You can remove the escapes also.

Here is a cleaned up version of TerryAtOpus' modification (I moved the dash to the end of the character class so it wouldn't need to be escaped):
// I assume this is PHP or PERL, so the forward slashes probably
//  still need to be escaped since I see them escaped in your OP

^http:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9.?,'\/\\+&;%$#_-]*)?$

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now