0

I've a url and a regex. I want to check whether the url matches the regex or not.

I'm checking it on Amazon product urls. Amazon Product URLs have dp in their urls. For eg:-

https://www.amazon.in/product-slug/dp/B07655JJYT
https://www.amazon.in/product-slug/some/path/dp/B00W9E8O80/ref?query_param=value
https://www.amazon.in/dp/B00W9E8O80/

I want to match all the above-given type of URLs.

Below is my code:-

import re

url = 'https://www.amazon.in/Perfect-Pricee-Cupcake-Steel-Muffin/dp/B07655JJYT'
pattern = "^*://www.amazon.in/*/dp/*$"

if re.search(pattern, url):
    print(1)

I'm not well versed with regexes. Not sure why it throws an error.

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • 1
    The regex for "any sequence of any length" is not `*`, but `.*` – khelwood Jun 19 '19 at 06:47
  • @Allan - It works. Could you also please publish this as an answer explaining what `.*(?<=/)` does? – Praful Bagai Jun 19 '19 at 07:01
  • @PythonEnthusiast: I want to help you more but your question is a duplicate so I can't post anymore answer. you can have a look at: https://regex101.com/r/Pfl3Uc/2/ for demo and explanations. – Allan Jun 19 '19 at 07:20
  • `^.*://www\.amazon\.in/.*(?<=/)dp/.*$` is better as `\.` will take the dot litteraly – Allan Jun 19 '19 at 07:22

0 Answers0