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)
  • Possible duplicate of [Find and replace string values in Python list](http://stackoverflow.com/questions/3136689/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](http://stackoverflow.com/questions/21341054/python-string-replace) – Bahrom Mar 28 '16 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" –  Mar 28 '16 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. –  Mar 28 '16 at 20:55
  • @HillaryDuff You could [`split("/")`](http://www.tutorialspoint.com/python/string_split.htm) the URL and later [`"/".join()`](https://stackoverflow.com/questions/1876191/explain-python-join) it again after taking out the parts you don't need. Check the links and experiment a bit. – jDo Mar 28 '16 at 20:59
  • @jDo, that method doesn't work because split() won't take colons, as used after http –  Mar 28 '16 at 21:05
  • 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 – twalberg Mar 28 '16 at 21:12
  • I was trying to get OP to experiment a bit and read the docs I provided (rather than handing out ready-made solutions) but everybody wants dem fake internet points. I was hoping that OP would eventually end up with @zondo's solution (that's why I suggested `split()` and `join()`) – jDo Mar 28 '16 at 21:27
  • @HillaryDuff `split()` will take any string/character, including colon. – jDo Mar 28 '16 at 21:38

2 Answers2

1

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

split_url = url.split("/")
split_url[4] = "goodbye"
url = "/".join(split_url)
zondo
  • 19,901
  • 8
  • 44
  • 83
  • 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! –  Mar 28 '16 at 21:19
  • @HillaryDuff: Oops! Typo. I fixed it ;) – zondo Mar 28 '16 at 21:24
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/'
Quinn
  • 4,394
  • 2
  • 21
  • 19