简体   繁体   中英

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 [AZ] 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM