Link to home
Start Free TrialLog in
Avatar of static86
static86

asked on

regexp - must contain "http://"

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?
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
__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