我用這從一個字符串中刪除除了「+」一切:
phone = re.sub(r'[^+]', '', cleaned_data['phone_number'])
我想這太:
phone = re.sub(r'[^\+]', '', cleaned_data['phone_number'])
它與'無效表達'失敗
編輯:使用調試器,如果發現錯誤位於這些行
phone_patterns = [r'^0\d{9}$', r'^\+33\d{9}$']
for phone_pattern in phone_patterns:
if re.match(phone, phone_pattern):
.....
stack_trace: 回溯:
File "/path/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/path/request_quote/views.py" in new
12. if formset.is_valid():
File "/path/local/lib/python2.7/site-packages/django/forms/formsets.py" in is_valid
277. err = self.errors
File "/path/local/lib/python2.7/site-packages/django/forms/formsets.py" in errors
259. self.full_clean()
File "/path/local/lib/python2.7/site-packages/django/forms/formsets.py" in full_clean
297. self._errors.append(form.errors)
File "/path/local/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
117. self.full_clean()
File "/path/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
273. self._clean_form()
File "/path/local/lib/python2.7/site-packages/django/forms/forms.py" in _clean_form
299. self.cleaned_data = self.clean()
File "/path/request_quote/forms.py" in clean
56. QuoteForm.COUNTRY_VALIDATORS[country](self, cleaned_data)
File "/path/request_quote/forms.py" in validate_fr
102. if re.match(phone, phone_pattern):
File "/path/lib/python2.7/re.py" in match
137. return _compile(pattern, flags).match(string)
File "/path/lib/python2.7/re.py" in _compile
242. raise error, v # invalid expression
Exception Type: error at /en/request_quote/new/
Exception Value: nothing to repeat
編輯: 錯誤來自於 '+' 開頭應該逃脫,但是怎麼樣?
EDIT2: 非常愚蠢的,但我沒有
re.match(string,pattern)
,而不是
re.match(pattern,string)
如果你想刪除的一切,爲什麼不'手機= '+''? –
無論如何,你的代碼有效。發佈完整的回溯並確保這是您正在運行的代碼。 –
「這樣的事情」?不,那個新的編輯仍然不會拋出你報告的錯誤(但是它會返回輸入'「+1234」')的'「+」'。那麼請不要請你發佈你使用的實際正則表達式? –