Skip to content

Instantly share code, notes, and snippets.

@marcusedu
Last active December 11, 2019 05:14
Show Gist options
  • Save marcusedu/11ceacc629c94ea1aa21a114e44bc55f to your computer and use it in GitHub Desktop.
Save marcusedu/11ceacc629c94ea1aa21a114e44bc55f to your computer and use it in GitHub Desktop.
Expressões regulares de uso comum
final RegExp email = RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]"
r"{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a"
r"-zA-Z0-9])?)*$",
caseSensitive: false,
multiLine: false);
final RegExp mobileBrazilianPhone = RegExp(r"^\(?0?\d{2}\)? ?9 ?\d{4}-?\d{4}$",
caseSensitive: false, multiLine: false);
final RegExp brazilianZipCode =
RegExp(r"^\d{5}[. -]?\d{3}$", caseSensitive: false, multiLine: false);
final RegExp dateMDY = RegExp(
r"^(1[012]{1}|0?[1-9])[/\-.]([12]{1}\d{1}|0?[1-9]{1}|[3]{1}[01]{1})[/\-.](\d{4}|\d{2})$",
caseSensitive: false,
multiLine: false);
final RegExp dateDMY = RegExp(
r"^[0123]?([123]{1}\d|0?\d)[/\-.](1[012]{1}|0?[1-9])[/\-.](\d{4}|\d{2})$",
caseSensitive: false,
multiLine: false);
final RegExp fullName = RegExp(r"^\w{2,} \w{2,}.*$");
final RegExp postalCode = RegExp(r"^\d{5,5}-?\d{3,3}$");
final RegExp cpf = RegExp(r"^\d{3,3}.?\d{3,3}.?\d{3,3}-?\d{2,2}$");
bool cpfValidate(String cpfTest) {
if (!cpf.hasMatch(cpfTest)) return false;
var digitsString = cpfTest.replaceAll(RegExp(r"\.|-"), "").split("");
if (digitsString.every((e) => cpfTest[0] == e)) return false;
var digitsNumbers = digitsString.map((s) => int.parse(s)).toList();
return _cpfAlgorithm(digitsNumbers.getRange(0, 9).toList(), digitsNumbers[9],
digitsNumbers[10]);
}
bool _cpfAlgorithm(List<int> numbers, int firstValidator, int secondValidator) {
int first = 0;
int second = 0;
int pos = 0;
numbers.forEach((number) {
first = number * (10 - pos) + first;
second = number * (11 - pos) + second;
pos++;
});
second = firstValidator * (11 - pos) + second;
if (first * 10 % 11 != firstValidator) return false;
if (second * 10 % 11 != secondValidator) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment