Skip to content

Instantly share code, notes, and snippets.

@titoaesj
Forked from igorcosta/cpf_cnpj_validator
Last active December 20, 2018 22:40
Show Gist options
  • Save titoaesj/a60495b4bbd2baccde46737951a12b7d to your computer and use it in GitHub Desktop.
Save titoaesj/a60495b4bbd2baccde46737951a12b7d to your computer and use it in GitHub Desktop.
Regex para validar CPF e CNPJ
Para CPF
/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/
fun main(args: Array<String>) {
if (Regex("^\\d{3}\\.\\d{3}\\.\\d{3}\\-\\d{2}$").matches("000.000.000-00")) {
println("sim")
} else {
println("não")
}
}
Para CNPJ
/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/
Para ambos ao mesmo tempo
/(^\d{3}\.\d{3}\.\d{3}\-\d{2}$)|(^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$)/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment