Need quick help on this regex..

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Need quick help on this regex..

Post by seodevhead »

I am building a regEx for URL validation and everything works great except one part. Here is my RegEx:

^((http)://)([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+_\.~\-]*)$

Problem is it is allowing this:

http://...mysite.com

**Notice the dots after the http://

Any idea how I can alter my RegEx to cure this? Thanks for your help and time!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I'm a complete regex noob too, but at first glance, it seems you just need to remove a blackslash-dot...

Code: Select all

^((http)://)([[:alnum:]\-])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+_\.~\-]*)$
I haven't tested it, so test it before you thank me, lol.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Wow it works! Thank you so very much. I am in debt.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

This is the one I use (but didnt write). I havn't been able to find a valid url that fails, or an invalid that passes...

Code: Select all

!^((http|https)\://)?([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*(localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/[a-zA-Z0-9\./_-]*[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]*)*/*$!
Post Reply