[英]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. 我正在做Python挑战,尽管我想出了一个谜题的答案,但我还是用一种不太擅长的方式做了。 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])
捕获一个小写字母。
[AZ]{3}
matches 3 uppercase letters (on both sides). [AZ]{3}
匹配3个大写字母(在两侧)。
[^AZ]
ensures that there isn't a 4th uppercase letter ( "EXACTLY three" ). [^AZ]
确保没有第4个大写字母( “完全是3” )。
[^AZ] a char that is not a capital letter [^ AZ]不是大写字母的字符
[AZ]{3} three capital letters [AZ] {3}三个大写字母
([az]) lowercase letter that you match 您匹配的([az])小写字母
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.