Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A Regex for simplifying Amazon product URLs

Actually, an Amazon product URL can also include a product title and have extra params after the ASIN. Example: https://www.amazon.com/Hitchhikers-Guide-Galaxy/dp/B0009JKV9W/ref=sr_1_1?crid=13ECW_...

posted 7mo ago by hkotsubo‭

Answer
#1: Initial revision by user avatar hkotsubo‭ · 2026-03-03T17:19:40Z (7 months ago)
Actually, an Amazon product URL can also include a product title and have extra params after the ASIN. Example:

```none
https://www.amazon.com/Hitchhikers-Guide-Galaxy/dp/B0009JKV9W/ref=sr_1_1?crid=13ECW_etc.....
                       ^^^^^^^^^^^^^^^^^^^^^^^^               ^^^^^^^^^^^^^^^
                         product description                  extra stuff
```

But that part is optional. For example, the same product above can be accessed by the URL `https://www.amazon.com/dp/B0009JKV9W`.

---

Anyway, the regex could be like this:

```none
https://www\.amazon\.com(?:/[^/]+)?/[dg]p/([a-zA-Z0-9]{10})
```

The `(?:/[^/]+)?` part matches the optional product title:

- `[^/]` means "any character that's not a slash"
- `+` means "one or more occurrences"

Therefore, `/[^/]+` is a slash followed by one or more characters that are not a slash. And it's all inside parenthesis, followed by `?` (which means "zero or one occurrence", AKA "optional").

Parenthesis normally create a capturing group, but the `(?:` syntax makes it a non-capturing group. I prefer to use this to indicate that I'm not interested in whatever matches that part of the regex, and it prevents the engine from creating groups that I don't want to look at.

Then `[dg]` matches either `d` or `g`, and it's followed by `p`.

And finally, `[a-zA-Z0-9]` matches any lowercase or uppercase ASCII letter, or any digit between 0 and 9. `{10}` means "exactly 10 occurrences". And everything is inside parenthesis, to create a capturing group, whose value you can easily get by using any engine/library.

For example, in Python:

```python
import re

url = ... # some URL

if match := re.match(r'https://www\.amazon\.com(?:/[^/]+)?/[dg]p/([a-zA-Z0-9]{10})', url):
    # get capturing group 1
    asin = match[1]
```

As groups are indexed in the order they appear in the expression, the information you want to extract will be in group 1 (as this is the first capturing group of the regex).

You can also check [here](https://regex101.com/r/5p72uY/1) an example of this regex working.

---

The [other answer](/posts/289889/289892#answer-289892) used `A-z` to match ASCII letters, but this interval also matches the characters `[`, `\`, `]`, `^`, `_` and ``` ` ```. Using `a-zA-Z`, you guarantee that only ASCII letters are matched.


---

That said, regex isn't exactly the best tool for URL parsing. Of course for specific, controlled cases (such as "_I know it's always an Amazon product URL, which has a well defined structure_"), it works fine. But for more general cases, there are speciliazed libraries/modules/functions that can parse, validate, handle tons of corner cases, and so on (things that can be harder to solve with a regular expression).