Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

401
Views
python (django) regex limit result not working

The spec is all characters except white-space and capitalized characters.

Here is the regex validator on the model:

path = models.CharField(max_length=150, help_text="/blog/posts/...", unique=True, validators=[
        RegexValidator(regex='(^[a-z0-9-:!@#$%^&*(){}\\?<>,.;\'"`~|/]+){1}', message="Lowercase with no whitespace allowed")
    ])

Here is the unit test:

def test_path_regex(self):
    with self.assertRaises(ValidationError):
        post = Post(title="bad regex", path="Super-2Test-2", slug="special")
        if post.full_clean():
            post.save()

    with self.assertRaises(ValidationError):
        post = Post(title="bad regex", path="super-2test 2", slug="special")
        if post.full_clean():
            post.save()

    self.assertEqual(Post.objects.filter(title="bad regex").count(), 0)

Result:

line 27, in test_path_regex
    post.save()
AssertionError: ValidationError not raised

FAILED (failures=1)

the reason for this seems to be that the RegexValidator is not flagging text if it can match part of the string and {1} is not helping. As you can see from this link: http://pythex.org/

Please help. Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The spec is all characters except white-space and capitalized characters.

You can simply use Negated Character Class. In your case it will be [^\sA-Z]. This will match all characters except whitespace and Upper Case alphabets

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!