Michael Denny's Blog ;]


Tutto quello che non avresti mai voluto sapere sulle Regular Expression...e se sei qui ti tocca!
Archivio Posts
Anno 2015

Anno 2014

Anno 2013

Anno 2012

Anno 2010

Anno 2008

Anno 2007

[RegEx In Practice] - Controllare la complessità di una password

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"
  • 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"
  • 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 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! 
mercoledì, 20 mar 2013 Ore. 14.48
Calendario
aprile 2017
lmmgvsd
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567









Statistiche
  • Views Home Page: 41.585
  • Views Posts: 51.442
  • Views Gallerie: 0
  • n° Posts: 30
  • n° Commenti: 16
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003