I have following image url:
https://img.com/woo/image/u/f_auto,q_auto/w_350/v1619377072/Products/dada/Products/dada/IMG_47473065_IDW_IMAGE_bf30ac4dbbd308c8b0248b954a58a731a5b413b0f6782b834c781d0da621d727_HR.jpg
Here url containing by default resolution i.e. w_350
in it. I want to remove this resolution from this image url.
Final output of image url should be like this :
https://img.com/woo/image/u/f_auto,q_auto/v1619377072/Products/dada/Products/dada/IMG_47473065_IDW_IMAGE_bf30ac4dbbd308c8b0248b954a58a731a5b413b0f6782b834c781d0da621d727_HR.jpg
How can I do this?
Is there anything better than this?
re.sub(r"w_\d\d\d/", "", url)
CodePudding user response:
This is another approach: If you are sure the pattern will only contain digits 0-9
it's always better to use [0-9]
rather than \d
, as from the documentation:
\d
Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched.
Using [0-9]
will reduce the computation as only the digit 0-9
will be matched and not all the Unicode characters.
import re
url = 'https://img.com/woo/image/u/f_auto,q_auto/w_350/v1619377072/Products/dada/Products/dada/IMG_47473065_IDW_IMAGE_bf30ac4dbbd308c8b0248b954a58a731a5b413b0f6782b834c781d0da621d727_HR.jpg'
url = re.sub(r'[a-z]_[0-9]{3}/', '', url)
print (url)
Output:
https://img.com/woo/image/u/f_auto,q_auto/v1619377072/Products/dada/Products/dada/IMG_47473065_IDW_IMAGE_bf30ac4dbbd308c8b0248b954a58a731a5b413b0f6782b834c781d0da621d727_HR.jpg
CodePudding user response:
I just tested and this worked
import re
pattern = "([a-z]_[0-9]{3}\/)"
original_string = "https://img.com/woo/image/u/f_auto,q_auto/w_350/v1619377072/Products/f6782b834c781d0da621d727_HR.jpg"
changed = re.sub(pattern, "", original_string)
#output
should give you this value
"https://img.com/woo/image/u/f_auto,q_auto/v1619377072/Products/f6782b834c781d0da621d727_HR.jpg"