RegEx Question for Strong Password

Jassim Al Rahma 1,616 Reputation points
2021-04-12T11:59:24.397+00:00

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

Developer technologies .NET .NET Runtime
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-04-12T18:06:12.447+00:00

    Based on new details, try another expression:

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

2 additional answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2021-04-12T13:09:42.217+00:00

    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.


  2. Gary Nebbett 6,216 Reputation points
    2021-04-12T20:56:32.573+00:00

    Hello @Viorel ,

    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 @Jassim Al Rahma 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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.