Richiesta: "devo controllare che una password sia almeno lunga 6 caratteri e al suo interno ci sia almeno 1 numero, una lettera maiuscola e una minuscola, hai una regex per questo?"
![]()
![]()
Risposta: "assolutamente si, è quello che ci vuole!" ![]()

Eccola:
\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])\S{6,}\z
Descrizione della regular expression (uso un software, RegexBuddy, non sono così bravo, ed è in inglese, non vi faccio la traduzione!
):

- Assert position at the start of the string
- Assert that the regex below can be matched, starting at this position (positive lookahead)
- Match a single character present in the list below
- Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
- One of the characters "-_"
- A character in the range between "a" and "z"
- A character in the range between "A" and "Z"
- A character in the range between "0" and "9"
- Match a single character in the range between "A" and "Z"
- Match a single character present in the list below
- Assert that the regex below can be matched, starting at this position (positive lookahead)
- Match a single character present in the list below
- Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
- One of the characters "-_"
- A character in the range between "a" and "z"
- A character in the range between "A" and "Z"
- A character in the range between "0" and "9"
- Match a single character in the range between "a" and "z"
- Match a single character present in the list below
- Assert that the regex below can be matched, starting at this position (positive lookahead)
- Match a single character present in the list below
- Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
- One of the characters "-_"
- A character in the range between "a" and "z"
- A character in the range between "A" and "Z"
- A character in the range between "0" and "9"
- Match a single character in the range between "0" and "9"
- Match a single character present in the list below
- Match a single character that is a "non-whitespace character"
- Between 6 and unlimited times, as many times as possible, giving back as needed (greedy)
- Assert position at the very end of the string

Codice C#:
void Main()
{
string inputString = "asd2Df";
bool foundMatch = false;
foundMatch = Regex.IsMatch(inputString, "\\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])\\S{6,}\\z");
Console.WriteLine(foundMatch);
inputString = "asdjDf";
foundMatch = false;
foundMatch = Regex.IsMatch(inputString, "\\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])\\S{6,}\\z");
Console.WriteLine(foundMatch);
}

Ciao!