Solved

regexp - must contain "http://"

Posted on 2011-03-14
2
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

617 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