Last active
February 18, 2025 18:07
-
-
Save salesHgabriel/c9c88be0b6502fa0db7699e5281db2b3 to your computer and use it in GitHub Desktop.
Exemplo que gera codigo no padrão ^[a-zA-Z0-9]{26,35}$ = A1B2C3D4E5F6G7H8I9J0KLMNOPQ ou AbcDefGhiJklMnoPqrStuVwxYz0123456789
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
class Program | |
{ | |
static Random random = new Random(); | |
static string pattern = "^[a-zA-Z0-9]{26,35}$"; | |
static string GenerateUniqueCode() | |
{ | |
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
int length = random.Next(26, 36); // Gera um tamanho aleatório entre 26 e 35 | |
StringBuilder result = new StringBuilder(length); | |
for (int i = 0; i < length; i++) | |
{ | |
result.Append(chars[random.Next(chars.Length)]); | |
} | |
return result.ToString(); | |
} | |
static void Main() | |
{ | |
for (int i = 0; i < 5; i++) // Gerar 5 códigos de exemplo | |
{ | |
string code = GenerateUniqueCode(); | |
if (Regex.IsMatch(code, pattern)) | |
{ | |
Console.WriteLine(code); | |
} | |
else | |
{ | |
Console.WriteLine("Código inválido gerado, tentando novamente..."); | |
i--; // Tenta gerar um novo código se o atual for inválido | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment