Solved

regexp - must contain "http://"

Posted on 2011-03-14
2
264 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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now