2

I need to replace https://www. , http://www. , http:// and https:// using Python3 regex for the below inputs;

Input:

 https://www.example.com/
 http://www.example.com/
 https://example.com/
 http://example.com/

Required output :

 example.com

Can anyone help me please since I am a beginner in Python.

3
  • 1
    Possible duplicate of regex and replace on string using python Commented Oct 29, 2018 at 12:57
  • it can be possible in your scenario https://example.com/test/? Commented Oct 29, 2018 at 14:29
  • If regex is not a requirement, you could just split the string like 'https://www.example.com/'.split('//')[1].split('/')[0] or look into urllib.parse. Commented Oct 29, 2018 at 14:47

3 Answers 3

1
import re
string = "http://www.example.com"
pat = re.compile("https?://www.")
print(len(string))
val = pat.match(string)
if not val is None:
    print(string[val.end():])
else:
    print("no match")

should work for you.if you want to start understanding re stands for Regular Expression in python and end returns index of the last match.

val is None if there is no match.

Sign up to request clarification or add additional context in comments.

5 Comments

I think it is required and change is not giving right results as s is an optional character that may appear once.
Oh yeah I see - then https? should do (optional single character)
What about the trailingslash at the end? that should be matched as-well right?
we can use split('/')[0] on the resulting string. I guess.
just see regex here. change it to [www]? for www
1

Thanks a lot for your suggestions and help. After going through the regex tutorials I managed to write the below code which gives me the expected result.

import re
domain = input("Enter the domain name : ")
stripped = 
re.sub(r'https://www\.|http://www\.|https://www|http://|https://|www\.', "", 
domain).strip('/')
print(stripped)

Comments

0
>>> import re
>>> m = re.search('example.com', a)
>>> if m:
...     print(m.group(0))

where a is the text variable

2 Comments

it will not work for other domains though. i.e.., it could be any other site-name
Please have a check on my code, I believe its a simple one and gives the expected result. Kindly let me if it fails at any situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.