---
title: "Python Challenge 3"
author: "finux"
date: "2011-04-26T13:28:33Z"
tags: []
categories: ["编程语言"]
slug: "python-challenge-3"
draft: false
lastmod: "2011-04-26T13:30:53Z"
id: 1018346
url: "https://finux.iteye.com/blog/1018346"
---

Python Challenge 3: 

[One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.](http://www.pythonchallenge.com/pc/def/equality.html)

 

大概的意思就是：有一个小写字母，它两旁有且仅有3个大写字母；然后把所有这些小写字母串接起来即可；

可举例：helloAAA**p**BBBworldCCC**y**DDDwhat，那么解析后结果即是py

仍然是一个文本处理的题目，个人觉得用正则表达式处理最方便不过了，呵呵。。。

表达式：re.compile('\[^A-Z]\[A-Z]{3}(\[a-z])\[A-Z]{3}\[^A-Z]', re.DOTALL)

 

那么代码就相当简单了~

 

```
#!/usr/bin/env python
#encoding = utf-8

import urllib
import re

def main():
        url = 'http://www.pythonchallenge.com/pc/def/equality.html'
        content = urllib.urlopen(url).read()
        reg = re.compile('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', re.DOTALL)
        result = ''.join(reg.findall(content))
        print result

if __name__ == '__main__':
        main()
```

 

运行结果就是linkedlist，那么把URL修改成XXX/linkedlist.php就是第4题了。

不知道还有没有更方便的处理方式呢？呵呵。。。