Question

I'm trying to match a url with a domain like:

Testing.com testing.com Testing.net testing.net Testing.org testing.org

and other extensions as well.

I'm trying to formulate a regex to use in a django view like:

(r'^Account/Testing/d=([a-z]{1,50})$', TestApp),

I tried ^[A-za-z]{2,50}$ but that doesn't match a domain with capital letter in the beginning

Any help?

Thank you!

Was it helpful?

Solution 2

Fortunately, this wasn't that bad after all - this is one way to match a domain with varying extensions:

^[A-za-z]{2,50}.[a-z]{1,3}$

matches .com, .org, .net, etc.

If you have a domain like me2.com, its better to use this:

(^[A-za-z0-9]{2,50}.[a-z]{1,3})$

OTHER TIPS

you can use this

/^(?:http(?:s)?:\/\/)?(?:w{3})\.([a-z_0-9-]+\.\w{2,3}(?:\.\w{2})?)/i

it will match for links likes this

http://www.site.com
https://www.site.com
http://www.site.co.uk
https://www.site.co.uk
http://www.site.com.br
https://www.site.com.br
http://www.site-site.com.br
https://www.site-site.com
http://www.site-site.co.uk
https://www.site-site.co.uk
www.site-site.com
www.site-site.co.uk
www.site-site.com.br
www.site.com

and alot of other variations

even if the site has
www.site.com/news

it will only match for "site.com"

the /i modifier will match for all variations of upper and lower cases

if you only want to match domain name as upper and lower

/^(?:http(?:s)?:\/\/)?(?:w{3})\.((?i:[a-z_0-9-])+\.\w{2,3}(?:\.\w{2})?)/

(?i:[a-z_0-9-]) will match variations for domain's names only

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top