>_ IzzyLab

Hands-on task

This task is step 32 of the course “Regular expressions”

Password check in a single expression

hard~10 min

/root/candidates.txt contains a list of candidate passwords. Using a single regular expression, select those that satisfy the policy all at once: contain an uppercase letter, contain a digit, contain at least one character that is not a letter or digit, and are at least 10 characters long. Write the result (in the original order) to /root/valid.txt.

What you'll practise:
  • grep -P
  • lookahead
  • (?=...)
  • zero-width assertion
  • {n,} quantifier

Topic breakdown

A lookahead (?=...) checks that matching text follows further along the line but leaves the parsing position where it was — so several such checks in a row form a conjunction of independent conditions, regardless of their order in the line. This is the standard technique for "the password must contain A and B and C" validation: list the conditions as lookaheads and leave the actual consumption of characters to the final part of the pattern. It works only in PCRE (grep -P, perl, most languages), not in grep's POSIX modes.

What's checked

  1. correct passwords selected in the original order
  2. no extra lines

What it looks like