1

It might be a silly question but i can not get it working.

import re
def name_validator(value):
    reg = re.compile(r'^\D*&')
    if not reg.match(value):
        raise ValueError

I want to match any string that do not contains digits. But it always raise ValueError.

>>> import re
def name_validator(value):
    reg = re.compile(r'^\D*&')
    if not reg.match(value):
        raise ValueError
>>> name_validator('test')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 5, in name_validator
ValueError
2
  • It's worth using something like regex101.com/#python to help you catch these mistakes early. Commented Mar 7, 2016 at 12:05
  • yeah. typo.. thanks guys :) Commented Mar 7, 2016 at 16:15

1 Answer 1

2

I want to match any string that does not contains digits.

The \D matches a non-digit symbol. A ^\D*$ matches an empty string and any string without digits inside.

You need to use

reg = re.compile(r'\D*$') # Note  DOLLAR symbol at the end
if not reg.match(value):

Or

reg = re.compile(r'^\D*$') # Note the CARET symbol at the start and the DOLLAR symbol at the end
if not reg.search(value):
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.