-1

Being new to Regex, I am working on a project that allows me to check if a password contains lowercase characters, uppercase and numerical ones. Here is the code:

text = "azeAZE123"
compilealpha = re.compile(r'[a-z]*')
compileAlpha = re.compile(r'[A-Z]*')
compilenum = re.compile(r'[0-9]*')
checkalpha = compilealpha.findall(text)
checkAlpha = compileAlpha.findall(text)
checknum = compilenum.findall(text)
print(checkAlpha)
print(checkalpha)
print(checknum)

What I do not understand is that I get an output like this one:

['', '', '', 'AZE', '', '', '', '']
['aze', '', '', '', '', '', '', '']
['', '', '', '', '', '', '123', '']

Could anyone explain to me what happened and what am I doing wrong please?

Mehdi RH
  • 322
  • 1
  • 7
  • 18
  • Try replacing the `*` by a `+` for having at least one character. The `*` stands for *0 or more of the preceding expression*. What is the desired output ? – Alexandre B. Apr 25 '20 at 15:07

1 Answers1

0

Your regular expressions specify the quantity specifier *, 0 or more matches. When combined with findall(), your regular expression matches empty substrings as well.

If you want to check if the regular expression has one or more matches, use r'[A-Z]+' instead.

Since you might be more interested in whether you have a match, and less interested in what that match is, you might consider using the regexp search() function instead of findall(), which will evaluate to a boolean.

erik258
  • 14,701
  • 2
  • 25
  • 31