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

206
Vistas
regex to restrict special characters except few

I was able to write a regex to validate the below criterias for an input box.

  1. contain minimum of 14 characters
  2. contain at least 1 capital letter (A-Z)
  3. contain at least 1 simple letter (a-z)
  4. contain at least 1 number (0-9)
  5. contain at least 1 special symbol (+=!@#$%^&*)

Regex -> ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[+=!@#$%^&*])(?=\\S+$).{14,}$

However, this regex allows other special characters which are not mentioned. I want to restrict all the special characters except these +=!@#$%^&*

could someone help me to modify the given regex with the above criteria?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Instead of . that allows any character you can use a character set to restrict the allowed characters to a specific set:

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[+=!@#$%^&*])(?=\\S+$)[0-9A-Za-z+=!@#$%^&*]{14,}$
about 4 years ago · Juan Pablo Isaza Denunciar

0

Why not use sets instead of a regular expression:

import string
upperSet   = set(string.ascii_uppercase)
lowerSet   = set(string.ascii_lowercase)
numberSet  = set("0123456789")
specialSet = set("+=!@#$%^&*")
allSets    = [upperSet,lowerSet,numberSet,specialSet] 
validSet   = set().union(*allSets)

isValid = len(pw)>=14 and validSet.issuperset(pw) \
          and all(s.intersection(pw) for s in allSets)

You could also use the translate method to convert each group of character into four codes that you then check to be exactly present in the translated string:

validCheck = str.maketrans(string.ascii_letters + "0123456789" + "+=!@#$%^&*",
                           "1"*26+"2"*26+"3"*10+"4"*10)

isValid = len(pw)>=14 and set('1234') == set(pw.translate(validCheck))))
about 4 years ago · Juan Pablo Isaza 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