1
resposta

[Projeto] Minha resposta à essa questão:

# You work in the customer service department of a company and need to quickly
# verify whether customers are entering their CPF numbers in the correct format
# before registering the data in the system.
#
# The expected CPF format is: three blocks of 3 digits separated by dots (.),
# followed by a block of 2 digits separated by a hyphen (-).
#
# Your task is to write a program that prompts the user for a CPF number and
# checks whether it is in the correct format.
#
# Input example:
#   Enter the CPF in the format XXX.XXX.XXX-XX: 123.456.789-00
#
# Expected output:
#   The CPF is in the correct format.

import re

cpf = input("Enter the CPF in the format: XXX.XXX.XXX-XX: ")
if re.match(r'\d{3}\.\d{3}\.\d{3}-\d{2}', cpf):
    print(f"The CPF {cpf} is in the correct format")
else:
    print(f"The CPF {cpf} is not in the correct format. Please enter a valid CPF.")
1 resposta

Oi, Raul! Como vai?

Agradeço por compartilhar

Gostei da sua solução: você usou regex de forma correta para validar a estrutura do CPF no formato XXX.XXX.XXX-XX. Um ajuste importante é usar ^ e $ na expressão regular, garantindo que o texto inteiro siga esse formato, sem aceitar caracteres extras antes ou depois: r'^\d{3}\.\d{3}\.\d{3}-\d{2}$'.

Dica: use re.fullmatch() quando quiser validar se toda a entrada segue exatamente o padrão esperado. Ficaria assim: if re.fullmatch(r'\d{3}\.\d{3}\.\d{3}-\d{2}', cpf):. Continue praticando, você está no caminho certo ao aplicar regex para resolver validações do dia a dia.

Alura Conte com o apoio da comunidade Alura na sua jornada. Abraços e bons estudos!