static86
asked on
regexp - must contain "http://"
Hi,
I want to validate url using regular expression.
I have found this code:
^http://\\/\/[0-9a-zA-Z]([-.\w]*[0-9a- zA-Z])*(:( 0-9)*)*(\/ ?)([a-zA-Z 0-9\-\.\?\ ,\'\/\\\+& amp;%\$#_] *)?$
is that correct?
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\-\.\?\,\'\/\\\+&%\$#_]*)?$
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-
is that correct?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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):
Open in new window