Learn lookahead regular expression by testing a strong password pattern

Tags: regex

regex

Strong password regex pattern

^(?=.*[A-Z]+)(?=.*[a-z]+)(?=.*\d+)(?=.*[\^\$!%@#]+)[\w\^\$!%@#]{8,12}$

What we we expected a password to match this pattern. Password contains 8-12 characters. Must contains at least:

  • one uppercase letter A-Z
  • one lowercase letter a-z
  • one digit 0-9
  • one special character, which are ^, $, !, %, @, #
  • support only English alphabet

In this article we will walk thought how to test if $0bA1234 matches our string password pattern and learing lookahead.

Before we start, we need to understand stand what "Lookahead" is.

Lookahead matches characters but returns only matched position, not a matched string. On the other hand, there is no matched string return. It is the same as "start and end of line ^, $" and "start and end of word \b". Syntax of lookahead

(?=test-pattern)

Let's break down a pattern and work on small pieces.

Lest's start.

Match ^

  • Start at the beginning of the string. $0bA1234
  • The caret ^ matches the position before the first character in the string.
  • Exit "start of line" and revert to position 0 in the string.

Match result

{
    "content": "",
    "isParticipating": true,
    "groupNum": 0,
    "groupName": null,
    "startPos": 0,
    "endPos": 0
}

Enter the first lookahead (?=.*[A-Z]+)

  • Start from the beginning of the string again. $0bA1234
  • Match $, 0, b as any single character with any of time.
  • Match character A as an uppercase letter.
  • Lookahead succeeds, exit and return the position 0.

Match result

{
    "content": "",
    "isParticipating": true,
    "groupNum": 0,
    "groupName": null,
    "startPos": 0,
    "endPos": 0
}

Enter the second lookahead (?=.*[a-z]+)

  • Start from the beginning of the string again. $0bA1234
  • Match $, 0 as any single character with any of times.
  • Match character b as a lowercase letter.
  • Lookahead succeeds, exit and return the position 0

Match result

{
    "content": "",
    "isParticipating": true,
    "groupNum": 0,
    "groupName": null,
    "startPos": 0,
    "endPos": 0
}

Enter the third lookahead (?=.*[\d]+)

  • Start from the beginning of the string again. $0bA1234
  • Match $ as any single character with any of times.
  • Match 0 as any digit.
  • Lookahead succeeds, exit and return the position 0

Match result

{
    "content": "",
    "isParticipating": true,
    "groupNum": 0,
    "groupName": null,
    "startPos": 0,
    "endPos": 0
}

Enter the last lookahead (?=.*[\^\$!%@#]+)

  • Start from the beginning of string again. $0bA1234
  • Match empty string as any single character with any of times of .*.
  • Match $ as a character in set ^$!%@#.
  • Lookahead succeeds, exit and return the position 0

Match result

{
    "content": "",
    "isParticipating": true,
    "groupNum": 0,
    "groupName": null,
    "startPos": 0,
    "endPos": 0
}

Enter main pattern [\w\^\$!%@#]{8,12} that captures full string

  • Start from the beginning of string again. $0bA1234
  • Match all string because of all single characters are in set:
  • \w which is [A-Za-b0-9]
  • Special characters which are ^$!%@#
  • The length matches between 8 to 12 times

Match result

{
    "content": "$0bA1234",
    "isParticipating": true,
    "groupNum": 0,
    "groupName": null,
    "startPos": 0,
    "endPos": 8
}

Match end of line $

  • Matches the position after the last character in the string

Match result

{
  "content": "",
  "isParticipating": true,
  "groupNum": 0,
  "groupName": null,
  "startPos": 8,
  "endPos": 8
}

Full match

  • Combine the result of each part, then we get a full match.

Match result

{
  "content": "$0bA1234",
  "isParticipating": true,
  "groupNum": 0,
  "groupName": null,
  "startPos": 0,
  "endPos": 8
}

To summarize, our test string matches a strong password pattern.