So the string url I have is 'http://helloworld.net/b/c/3?nw=174057&csid=fire.clan.com/gram/&din=&rand=52710&w=400&h=112'
. This is in python 2.7.
Now the parameter rand in the url keeps changing and is always a 5 number length so it can be rand=12345
or 99999
and etc
Now I want to change that in this case ( 'rand=52710'
) to something like 'sub'
or just ''
I have made attempts at this by using https://developers.google.com/edu/python/regular-expressions
but didn't come up with a solid solution
Any help would be appreciated thanks.
Using Regex .
Ex:
import re
u = 'http://helloworld.net/b/c/3?nw=174057&csid=fire.clan.com/gram/&din=&rand=52710&w=400&h=112'
print re.sub("rand\=\d+", "SUB", u)
Output:
http://helloworld.net/b/c/3?nw=174057&csid=fire.clan.com/gram/&din=&SUB&w=400&h=112
Without regex, assuming that your claim " now the parameter rand in the url keeps changing and is always a 5 number length so it can be rand=12345 or 99999 " holds true:
url = "http://helloworld.net/b/c/3?csid=fire.clan.com/gram/&din=&rand=52710&w=400&h=112"
index = url.rfind("rand=")
if index != -1:
url = url[:index] + url[index+11:] # add 'sub' between url parts if you want it
# http://helloworld.net/b/c/3?csid=fire.clan.com/gram/&din=&w=400&h=112
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.