Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A Regular Expression (Regex), as you will find out, is a tool used in matching text against a pattern and even extracting or replacing matches in the source text.</p> <p>To do this the Regex engine (which exists in the .Net framework inside the namespace System.Text.RegularExpressions) uses some established patterns that represent certain kinds of chars.</p> <p>To use a Regex, you pass it the pattern agains which a text will be tested and the text itself.</p> <p>For instance, the following snippet tests if there are lowercase letters in the text:</p> <pre><code>using System.Text.RegularExpressions; ... var Pattern = "[a-z]"; if (Regex.IsMatch(SomeText, Pattern)) { //... there is at least one lower case char in the text } </code></pre> <p>The pattern used above estates that we are interested in a range of chars from lowercase "a" to lowercase "z".</p> <p>A pattern to require at least one lowercase letter, one digit and one uppercase letter could probably be something like</p> <pre><code>@"[\d]+[a-z]+[A-Z]|[\d]+[A-Z]+[a-z]|[a-z]+[\d]+[A-Z]|[a-z]+[A-Z]+[\d]|[A-Z]+[\d]+[a-z]|[A-Z]+[a-z]+[\d]" </code></pre> <p>As you can see, a Regex pattern can be as simple as in the first example, but may escalate fast in complexity depending on the kind of information you want to check or extract.</p> <p>In the Regex above, the items between square brackets are called character classes. The plus sign after an element is a quantifier, it indicates that the item may appear one or more times. Items separated by a vertical bar indicate alternatives. The "\d" pattern represents any digit from "0" to "9".</p> <p>You don't really need a Regex to check the password strength in your application, but using then instead of the explicit tests you are currently making would (in my opinion), greatly improve your program's readability:</p> <pre><code>using System.Text.RegularExpressions; ... if (!Regex.IsMatch(password, "[A-Z]")) { errorMessage = "Your password must contain at least one uppercase character."; } else if (!Regex.IsMatch(password, "[a-z]") { errorMessage = "Your password must contain at least one lowercase character."; } else if (! Regex.IsMatch(password, @"[\d]")){ errorMessage = "Your password must contain at least one numerical character."; } ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload