0

How to use python to replace 'http://xyz.example.com' to 'http://example.com' with regular expression

Note: 'xyz' is just a template. it may be '123' or 'abc-123'

4
  • There is probably a library to strip the subdomain from the URL, if that's what you're looking for Commented May 25, 2017 at 15:47
  • I would not use re for this, rather urllib.parse, which is made specifically to parse and create URLs Commented May 25, 2017 at 15:48
  • stackoverflow.com/questions/14406300/… Commented May 25, 2017 at 15:50
  • 1
    Is regex really a requirement? You dont need them in this case. Commented May 25, 2017 at 15:51

1 Answer 1

3

This would do it:

import re

input = 'http://xyz.example.com'

output = re.sub(r'(?<=http:\/\/).*?\.', '', input)

print(output)

Regex demo
Python demo

  1. (?<=http:\/\/) is a positive look behind for http://
  2. .*?\. matches everything that isn't a new line token lazily up until the first .
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.