【问题标题】:Regex for the django form fielddjango 表单字段的正则表达式
【发布时间】:2012-03-22 17:06:00
【问题描述】:
我有一个表单域:
web_address = forms.RegexField(regex=r'^DO SOMETHING$', error_messages = {'invalid': _("Not a valid web address.")})
我无法为上述地址编写正则表达式。我的要求是以http://maps.google.com/maps/place?cid=1234 的形式验证网址。 id 1234 是一个变体,但先验是不变的。
请帮忙!!!
【问题讨论】:
标签:
python
regex
django
django-forms
【解决方案2】:
这应该可行:
web_address = forms.RegexField(regex=r'^http\\:\\/\\/maps\\.google\\.com\\/maps\\/place\\?cid\\=\d+'$', error_messages = {'invalid': _("Not a valid web address.")})
IE:
>>> import re
>>> re.match('^http\\:\\/\\/maps\\.google\\.com\\/maps\\/place\\?cid\\=\d+', 'http://maps.google.com/maps/place?cid=1234', re.IGNORECASE)
<_sre.SRE_Match object at 0x1251fa8>