question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked JassimAlRahma-9056 commented

RegEx Question for Strong Password

Hi,

I need help with this RegEx please.

I want to have a strong password with the following:

  1. Minimum 8 characters, Maximum 15

  2. Must Start with Alpha

  3. Must have mixed Alpha-Numeric

  4. Can have capital and small Anywhere and no necessary in the beginning [but not must]

  5. Can have special characters [but not must]




Kindly help..


Thanks,
Jassim

dotnet-csharpdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered JassimAlRahma-9056 commented

Based on new details, try another expression:

 (?i)^(?=[a-z])(?=.*[0-9])([a-z0-9!@#$%\^&*()_?+\-=]){8,15}$


· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @Viorel-1,

This is really nit-picking of me (especially since the Unicode value of '=' is less than that of '\'), but you have placed the '-' not at one of the ends of the character group. This could have led to unintended consequences...

Gary

0 Votes 0 ·

Since ‘-‘ is preceded by ‘\’, it is a simple ‘-‘, not a range of characters.

0 Votes 0 ·

Hello @Viorel-1,

Sorry to bother you again, but are you sure? Is it not the case that restricted escaping rules apply in positive character groups?

I just made these tests to confirm (my possibly tired and unreliable) expectations:

 Console.WriteLine(Regex.IsMatch(@"[\t]", @"\t"));
 Console.WriteLine(Regex.IsMatch(@"[\t]", @"t"));
 Console.WriteLine((int)@"\t"[0]);
 Console.WriteLine((int)"\t"[0]);


with the results:

 False
 True
 92
 9

I.e.

  1. a tab does not match the positive character group [\t]

  2. a "t" does match [\t]

Gary

0 Votes 0 ·
Show more comments
Viorel-1 avatar image Viorel-1 JassimAlRahma-9056 ·

The expression was made for C# (Regex class), not for JavaScript.


2 Votes 2 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered JassimAlRahma-9056 commented

Check this expression:

 ^(?=.{8,15}$)(?=\p{L})(?=.*\p{N}.*$).*

If it does not work, or "special character" means something specific, then show the code and sample passwords.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

or "special character" means something specific


Just the standard special characters, !, @, #, $, %, ^, &,*, (, ), _, ?, +, -, =

Forgot to mention No Space

Here are sample of valid passwords:

  • unitedstates2020

  • unitedstates@#2020

  • uNiTedStAtEs2020=

  • United2020States%

here are sample of invalid passwords

  • United [less than 8]

  • United@#States?&2020 [more than 15]

  • United!@ States [it has a space]

  • UnitedStates [ No numeric]

  • 2020UnitedStates [starting with numeric]



0 Votes 0 ·
GaryNebbett avatar image
0 Votes"
GaryNebbett answered GaryNebbett edited

Hello @Viorel-1,

Oops! Sorry, Mea culpa, I got the order of the input and pattern arguments wrong.

This is a bit off-topic but possibly still relevant for @JassimAlRahma-9056 to create a bullet-proof regular expression.

I am now having difficulty identifying a definitive source of information for this topic. The .NET documentation says:

The following table lists the character escapes supported by regular expressions in .NET.

CHARACTER ESCAPES IN .NET
Character or sequence Description
All characters except for the following:

. $ ^ { [ ( | ) * + ? \ Characters other than those listed in the Character or sequence column have no special meaning in regular expressions; they match themselves.

The characters included in the Character or sequence column are special regular expression language elements. To match them in a regular expression, they must be escaped or included in a positive character group. For example, the regular expression \$\d+ or [$]\d+ matches "$1200".

Wikipedia says (in the "POSIX basic and extended" section):

A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].
The - character is treated as a literal character if it is the last or the first (after the ^, if present) character within the brackets: [abc-], [-abc]. Note that backslash escapes are not allowed. The ] character can be included in a bracket expression if it is the first (after the ^) character: []abc].

Gary

P.S.

I also just found this: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03_05

A bracket expression (an expression enclosed in square brackets, "[]" ) is an RE that shall match a specific set of single characters, and may match a specific set of multi-character collating elements, based on the non-empty set of list expressions contained in the bracket expression.

The following rules and definitions apply to bracket expressions:

A bracket expression is either a matching list expression or a non-matching list expression. It consists of one or more expressions: ordinary characters, collating elements, collating symbols, equivalence classes, character classes, or range expressions. The <right-square-bracket> ( ']' ) shall lose its special meaning and represent itself in a bracket expression if it occurs first in the list (after an initial <circumflex> ( '^' ), if any). Otherwise, it shall terminate the bracket expression, unless it appears in a collating symbol (such as "[.].]" ) or is the ending <right-square-bracket> for a collating symbol, equivalence class, or character class. The special characters '.', '*', '[', and '\\' ( <period>, <asterisk>, <left-square-bracket>, and <backslash>, respectively) shall lose their special meaning within a bracket expression.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.