vijay M
vijay M

Reputation: 45

python regular expression for URL just "www.example.com"

I need python regex for "www.example.com" (without quotes). example can be of any string. I need it without any other text before "www" and after ".com"

Upvotes: 0

Views: 838

Answers (1)

Tiger-222
Tiger-222

Reputation: 7150

You can use a dedicated function from the standard library urllib.parse.urlparse:

>>> from urllib.parse import urlparse
>>> parts = urlparse('http://www.example.org')
>>> parts
ParseResult(scheme='http', netloc='www.example.org', path='', params='', query='', fragment='')
>>> parts.netloc
'www.example.org'

Or you can use this regexp for a text:

>>> import re
>>> regexp = re.compile(r'\s*(www\.[^:\/\n]+\.com)\s*')
>>> urls = regexp.findall('Hello https://www.mywebsite.com/index.py?q=search bonjour...')
>>> urls
['www.mywebsite.com']

Upvotes: 1

Related Questions