我正在参加 Python 挑战赛,虽然我想出了一个谜题的答案,但我用的是一种笨拙的、不是很好的方法。前进后,我能够看到解决方案:
string1 = open('text.txt').read()
print ''.join(re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', string1))
我搞砸了一段时间,在这里删除了一个插入符,看看会发生什么,在那里更改了一个支撑组。但是,我只是无法理解为什么会这样。谁能用通俗易懂的方式解释一下?
谢谢!
最佳答案
我将模式编译为冗长的以包含内联注释:
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)
演示:
>>> re.findall(pat, 'AAAAaBBBbBBBBzzZZZxXXXyYYYYwWWWvABCn')
['x', 'v']
关于Python Challenge Lvl 3 解释(警告剧透!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10898651/