Mazki516
Mazki516

Reputation: 1027

writing '-' in regular expression django url

I want to accept in a django url:

StoreageLocation/X-YYY-X   (E-152-A , E-112-B)  

(where X is [A-Z] and Y is [0-9])

First I tried to solve for X-YYY with:

url(r'^StorageLocation/\[A-Z\-0-9{3}])$', views.StorageLocation, name='StorageLocation'),

but that won't work. Please help.

Upvotes: 0

Views: 70

Answers (1)

alecxe
alecxe

Reputation: 474221

Check one upper case letter, then dash, then exactly 3 digits, the dash and a single upper case letter:

url(r'^StorageLocation/[A-Z]\-[0-9]{3}\-[A-Z]$', views.StorageLocation, name='StorageLocation'),

Demo:

>>> import re
>>> pattern = re.compile(r'[A-Z]\-[0-9]{3}\-[A-Z]')
>>> pattern.match('E-152-A')
<_sre.SRE_Match object at 0x103aba6b0>
>>> pattern.match('E-112-B')
<_sre.SRE_Match object at 0x103abab90>

Upvotes: 1

Related Questions