1

I have a string that is holding a URL:

url = http://example.com/1234/hello/

I wish to replace "hello" with "goodbye"

Thank you in advance!

Best Regards

Edit: My apologies, the example I used had a lot of placeholders. For extra clarification, "1234" and "hello" are dynamic and change a lot, so I need a method to replace everything after the 4th "/", if you will.

So far I've tried this, but it deleted the slashes and numbers:

url = re.search(r'(\A.*)0/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)1/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)2/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)3/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)4/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)5/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)6/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)7/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)8/',url,re.DOTALL|re.IGNORECASE).group(1)
url = re.search(r'(\A.*)9/',url,re.DOTALL|re.IGNORECASE).group(1)
8
  • Possible duplicate of Find and replace string values in Python list. There strings are replaced in a list, you just need to do that for a single string. There are plenty of other string replacement examples here, here's another one Commented Mar 28, 2016 at 20:43
  • @Andrew I just editted my question to suit yours. The problem is, the sub directories on the URL are dynamic and subject to change, so I cannot just use code that changes "hello" to "goodbye" Commented Mar 28, 2016 at 20:53
  • @BAH I just editted my question. The problem is, the sub directories on the URL are dynamic and subject to change, so I cannot just use code that changes "hello" to "goodbye." The questions you linked only provide instances if the variables are uniform throughout the entire code. Commented Mar 28, 2016 at 20:55
  • @HillaryDuff You could split("/") the URL and later "/".join() it again after taking out the parts you don't need. Check the links and experiment a bit. Commented Mar 28, 2016 at 20:59
  • 1
    "/".join(url.split("/")[0:4] + ["goodbye"]) would be one rather simple method. Not sure what the comment about split() not taking colons means, @HillaryDuff Commented Mar 28, 2016 at 21:12

2 Answers 2

1

You can use .split() and .join():

split_url = url.split("/")
split_url[4] = "goodbye"
url = "/".join(split_url)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This worked perfectly! I switched the 3 to a 4, however, because I wanted the "hello" to be switched to "goodbye", not the 1234, but thank you so much!
@HillaryDuff: Oops! Typo. I fixed it ;)
0

Are you looking for this (python 2.7)?

>>> import re
>>> input = ' http://example.com/1234/hello/'
>>> re.sub('((?:.*?/){4})([^/]+)(/?)', r'\1goodby\3', input)
' http://example.com/1234/goodby/'

Comments

Your Answer

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