Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
igsm '10,440 Points
Boolean with re?
Hey. Can you help please with the following. Using RE, I want to check a string, which is meant to be a passwords. True if it contains >= 10 chars, at least one lower letter, one capital letter and one number; else False. How would a code look like. Thanks.
e.g. two cases:
True = 'bAse730onE4'
False = 'saaaa90'
Chris FreemanTreehouse Moderator 68,029 Points
Because you are looking for the
AND of multiple conditions there isn't a short single concise regexp that will solve your needs because your conditions are not order dependent. That is, you would have to check for all combinations of a condition followed by all others. For example lowercase followed by uppercase followed by number and being 10 characters long, plus the other 5 combinations,
ORed together in a single regexp.
The other option is to create the 3 search patterns and check that they each match plush a length check.
patterns =  patterns.append(re.compile(r'[a-z]+')) #lowercase patterns.append(re.compile(r'[A-Z]+')) #uppercase patterns.append(re.compile(r'[0-9]+') )# number password_good = True if len(password_string) < 10: password_good = False else: for pat in patterns: if not pat.search(password_string): # pattern not found password_good = False break
You could include the length check as a regexp:
I'll work on the combined uber-regexp to show it's complications and ammend my answer.
EDIT: Here is a single regexp pattern to match against. Yeah, it's not pretty:
In : patx = re.compile(r''' \w*[a-z]+\w*[A-Z]+\w*[0-9]+\w*| \w*[a-z]+\w*[0-9]+\w*[A-Z]+\w*| \w*[A-Z]+\w*[a-z]+\w*[0-9]+\w*| \w*[A-Z]+\w*[0-9]+\w*[a-z]+\w*| \w*[0-9]+\w*[a-z]+\w*[A-Z]+\w*| \w*[0-9]+\w*[A-Z]+\w*[a-z]+\w* ''', re.X) In : patx.search("") In : patx.search("a") In : patx.search("aA1") Out: <_sre.SRE_Match object; span=(0, 3), match='aA1'>
Now you can use:
if not len(password) > 10 and patx.search(password) print("Bad Password")
Hi Igor, re.search and re.match return match objects. If the item isn't found, it returns None (the special value). re.match checks for a match at the beginning of the string, whereas re.search checks anywhere in the string. You can use the boolean operator "and" to check multiple conditions--for example:
def check_two(string): return re.search(\w+) and re.search(\d+)
returns True if the string contains at least one word character ([a-zA-Z0-9_]) and at least one decimal digit. You can get more specific by using a set definition, like [a-z]+.