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

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

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 Relatório

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 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