import re good_urls = ( 'http://www.amazon.com/exec/obidos/ASIN/B004NVUXH2/gasmencommon', 'http://www.amazon.com/exec/obidos/ASIN/B004NVUXH2/gasmencommon-10', 'http://www.amazon.com/exec/obidos/ASIN/B004NVUXH2/gasmencommon-2a', ) bad_urls = ( 'http://www.amazon.com/exec/obidos/ASIN/B004NVUXH2/gasmencommon-20', 'http://www.amazon.com/exec/obidos/ASIN/B004NVUXH2/gasmencommon-20-sdsdf', 'http://www.amazon.com/exec/obidos/ASIN/B004N-20VUXH2/gasmencommon', ) #reg = re.compile(r'^.*(gp/product|dp|ASIN)/(B[A-Za-z0-9]+)((?!\-2\d).)*$') #reg = re.compile(r'^.*(gp\/product|dp|ASIN)/(?P<ext_id>B[A-Za-z0-9]+)((?!\-2\d).)*$') reg = re.compile(r'^.*(gp\/product|dp|ASIN)\/(?P<ext_id>B[A-Za-z0-9]+)(?!.*\-(20|21).*$)') def test_urls(): for url in good_urls: match = reg.search(url) assert match print match.groups()[1] for url in bad_urls: assert not reg.search(url) if __name__ == '__main__': test_urls()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question