<pythonchallenge.com>----Lv3

http://www.pythonchallenge.com/

一个有趣的python学习网站,就是在给出的图片和提示信息的帮助下,利用python解谜,然后进入下一关。

第三关:http://www.pythonchallenge.com/pc/def/equality.html

第三关的hint

One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

和第二关一样,在网页源码里的一堆字符串中选出特定的字符串:一个左右两边都有三个大写字母的小写字母,比如 aXXXbXXXc,那么b符合要求。

我的解决方法

# -*- coding: utf-8 -*-
import re
fp=open('/root/python/test.txt')
p=re.compile(r'[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', re.M)
s=''
for line in fp:
    L=p.findall(line)
    if L:
        for x in L:
            s=s+x[4]
print s

得到下一关的地址:http://www.pythonchallenge.com/pc/def/linkedlist.html

新手写的程序就是bug多,哈哈,看了别人的答案后觉得自己考虑得太少了。

如果行开头是XXXaXXXb,行结尾是aXXXbXXX那么我写得正则根本不能用。

我考虑改进如下

# -*- coding: utf-8 -*-
import re
fp=open('/root/python/test.txt')
p=re.compile(r'[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', re.M)
s=''
for line in fp:
    L=p.findall('a'+line+'a')
    if L:
        for x in L:
            s=s+x[4]
print s

 在每一行前后都加个小写字母。

一个网友的写法:(?<=[^A-Z][A-Z]{3})[a-z](?=[A-Z]{3}[^A-Z])', '^'+line)

原文地址:https://www.cnblogs.com/luobuda/p/pythonchallenge-Lv3.html