Quantcast
Channel: Home Page http://blogs.dotnethell.it
Viewing all articles
Browse latest Browse all 24464

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

$
0
0
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! 

Viewing all articles
Browse latest Browse all 24464

Trending Articles


FORECLOSURE OF REAL ESTATE MORTGAGE


FORTUITOUS EVENT


Pokemon para colorear


Sapos para colorear


Patama Quotes – Tanga love tagalog quotes


“BAHAY KUBO HUGOT”


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 11.5.1 by Vimeo.com, Inc.


UPDATE SC IDOL: TWO BECOME ONE


KASAMBAHAY BILL IN THE HOUSE


Girasoles para colorear


Long Distance Relationship Tagalog Love Quotes


Inggit Quotes and Taray Quotes


“Hanggang s apag tanda natin”


RE: Mutton Pies (frankie241)


Hato lada ym dei namar ka jingpyrshah jong U JJM Nichols Roy (Bah Joy) ngin...


Vimeo 10.7.1 by Vimeo.com, Inc.


Vimeo 11.8.1 by Vimeo.com, Inc.