简体   繁体   中英

Python Challenge Lvl 3 Explaination (warning spoilers!)

I am doing the Python challenge and while I figured out the answer to a puzzle, I did it in a hacky, not-very-good way. Upon advancing I was able to see the solution which is:

string1 = open('text.txt').read()
print ''.join(re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', string1))

I messed with this for a while, removing a caret here and seeing what happens, changing a braced group there. However, I just cant wrap my head around why this works. Could anyone explain it in a easy to understand way?

Thank you!

([az]) captures a single small letter.

[AZ]{3} matches 3 uppercase letters (on both sides).

[^AZ] ensures that there isn't a 4th uppercase letter ( "EXACTLY three" ).

[^AZ] a char that is not a capital letter

[AZ]{3} three capital letters

([az]) lowercase letter that you match

repeat the first two.

I compiled the pattern as verbose to include inline comments:

pat = re.compile('''
    [^A-Z]    # any character except a capital letter
    [A-Z]{3}  # three capital letters
    (         # the beginning of a capturing group
    [a-z]     # one lowercase letter 
    )         # the end of the group
    [A-Z]{3}  # three capital letters
    [^A-Z]    # any character except a capital letter
    ''', re.VERBOSE)

Demo:

>>> re.findall(pat, 'AAAAaBBBbBBBBzzZZZxXXXyYYYYwWWWvABCn')
['x', 'v']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM