-1

I have the following regular expression:

passw = re.compile(r'^(?=.*\d)(?=.*[A-Z])(?=.*[\$|\?\!])[A-Za-z\d$!&]{8}$')

which I can interpret as string of length 8, which must contain at least one number, one big letter and one of characters($?!). And also consists only of numbers, letters and $?!.

?= - this symbol is known as lookahead assertion: For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.

But how can we interpret .* sign? What does it mean in regex? Can we avoid it in this situation?

YNWA1992
  • 99
  • 1
  • 10
  • 2
    `.*` is not one sign. It is the `.` character (meaning "any character") and the `*` character (meaning "any number of those (including zero)"). So `.*` means "any number of any characters". – khelwood Jun 29 '16 at 12:03
  • `.*` matches any character (except newline) between zero and unlimited times. – Maria Ivanova Jun 29 '16 at 12:04
  • Is there a reason you're trying to do all those checks in a single regexp? Have you considered making two or three separate checks which are each easier to read and maintain than one long regex? – Bryan Oakley Jun 29 '16 at 12:05

1 Answers1

1

In fact .* means zero or more occurrences of any character, and when it follows with a special pattern in a lookahead it means that the regex engine will assert that the regex can be matched. And when you use .* before your pattern you are telling the regex engine to match my pattern that precede by zero or more characters this means you want to be sure that your pattern is present in your string (from leading to trailing).

Here is an example with diagram:

(?=.*\d)[a-z]+

Regular expression visualization

Debuggex Demo

kasravnd
  • 94,640
  • 16
  • 137
  • 166