Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

403
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda