Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

409
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda