Python Challenge Level 3
Entry: http://www.pythonchallenge.com/pc/def/equality.html
Requirement: One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.
思路:
要求是找出加在三个大写字母中间的一个小写字母。这个小写字母左右两个方向都有三个大写字母。一看就知道应该用正则表达式解决,那么这个正则表达式怎么写呢?
有且仅有一个小写字母:[a-z]
有且仅有三个大写字母:[A-Z]{3}那么这个Regular Expression的Pattern就是:[A-Z]{3}[a-z][A-Z]{3}
………………
不对。。在此我犯了一个小错误。上述表达式不能考虑到这种情况:ABCDaABCD。即三个大写字母再外侧是什么,也需要考虑进去。它们不应该是大写字母了。
所以还应该加上非大写字母的Pattern:[^A-Z]
So.… Pattern: [^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]
源代码:
content='''......'''
import re
import string
result=re.findall(r'[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]',content)
result=[l[4] for l in result]
result=string.join(result,'')
print result
结果:
linkedlist
r.findall(‘[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]’, content)
结果也是linkedlist
不过我试的http://www.pythonchallenge.com/pc/def/linkedlist.html无法打开,只显示linkedlist.php几个字样
卡到这里了,郁闷~~
[Reply]