2👍
✅
You can use
^images/(?:(?P<prefix>\w+)/)?(?P<uuid4>[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})\.(?P<ext>\w+)
See the regex demo
The optional dir after images/
is matched with an optional group (?:(?P<prefix>\w+)/)?
. The ?
quantifier matches 1 or 0 occurrences. If there can be more than 1, use *
instead of ?
(but I guess you’d have to think of correct “prefix” group boundaries).
Also, [0-9a-f][0-9a-f]{8}
in your regex requies 9 chars, but there are 8 in fact.
3 consecutive -[0-9a-f]{4}
can be just shrunk into another non-capturing group (?:-[0-9a-f]{4}){3}
.
NOTE: It might be a good idea to prepend the pattern with (?i)
(case insensitive modifier): (?i)^images/(?:(?P<prefix>\w+)/)?(?P<uuid4>[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})\.(?P<ext>\w+)
Source:stackexchange.com