美文网首页
Python Challenge[3]

Python Challenge[3]

作者: Recgat | 来源:发表于2017-02-07 15:23 被阅读0次

[Level 3]


Title: re

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

信息不足,同样查看源码,看到一堆字符串,且全是字母。根据提示,想到应该是两组三个大写字母夹着一个小写字母,使用正则表达式匹配求解。找出第一个字母时修改url转到的网页显示yes. but there are more.,需要找出所有符合条件的字母:

import re
pattern = re.compile('[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]')
target = ''
with open('re.txt',encoding='utf-8') as f:
  s = f.read()#.replace('\n','')
match = pattern.search(s)
while(match):
  temp = s[match.start():match.end()]
  target += temp[4]
  s = s[match.end()-1:]
  match = pattern.search(s)
print(target)

或直接在源码中读取目标字符串:

import urllib.request
s = urllib.request.urlopen('http://www.pythonchallenge.com/pc/def/equality.html').read().decode('utf-8').replace('\n','')
s = re.findall('<!--(\w*)-->',s)[0]

运行得到linkedlist,修改url网页提示linkedlist.php,即[Level 4]

小结

寻找到匹配的第一个字符后,再从该字符后三个字符(大写字母)继续开始匹配寻找。

  1. re.compile(pattern, flags=0)将正则表达式模式编译成可被match()search()方法调用的对象。
  2. regex.search(string[, pos[, endpos]])扫描string 寻找正则表达式产生匹配后的第一个位置 , 返回对应的 match object。
  3. match.start([group])match.end([group])返回由group匹配的子字符串的开头和结尾的索引。
  4. search() and match()

Python Challenge Wiki

不用re的解法

import string
def level_3(t1):
pad = "xx"
t1 = pad + t1 + pad
markers = "".join([ '1' if c in string.ascii_uppercase else '0' for c in t1])
pattern = "011101110"
def f(res, t2, markers):
n = len(markers.partition(pattern)[0])
return f(res+t2[n+4], t2[n+4:], markers[n+4:]) if n != len(markers) else res
return f('', t1, markers)


这个也挺有趣的:
> ```python
>>> import string
>>> code = data.replace("\n", "")
>>> word =''
>>> for i in range(len(code) - 8):
...     if [c for c in code[i:i+9] if c in string.ascii_lowercase] == [code[i], code[i+4], code[i+8]]:
...         word += code[i+4]
...
>>> word
'linkedlist'

将大小写字母全部转为1和0达到使用正则表达式的效果。

  1. str.partition(sep)在分隔符首次出现位置拆分成三个子字符串,并返以元组形式返回。未找到返回分隔符本身和两个空字符串组成的元组。

更优雅的使用re

>> import re
>> ''.join(re.findall('(?:^|[^A-Z])[A-Z]{3}([a-z])[A-Z]{3}(?:[^A-Z]|$)',text))

1. `(?:...)`在正则中表示匹配的子字符串不能在匹配后提取或在模式中引用。更多语法查看[Regular Expression Syntax](https://docs.python.org/3/library/re.html#regular-expression-syntax)
2. [`re.findall(pattern, string, flags=0)`](https://docs.python.org/3/library/re.html?highlight=findall#re.findall)返回所有非重叠匹配的模式。如果模式中存在一个或多个组,则返回组列表;如果模式中组有嵌套(不知这样理解对不),返回元组的列表。

####[More](http://wiki.pythonchallenge.com/index.php?title=Level3:Main_Page)

相关文章

  • Python挑战:00~03关

    Python Challenge Python Challenge 00 网址: http://www.pytho...

  • The Python Challenge(3)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面提示: 阅读源码,有如下内容: 编写代码从中...

  • Python Challenge[3]

    [Level 3] Title: re One small letter, surrounded by EXACT...

  • Python挑战:04-05关

    Python Challenge Python Challenge 04 现在,我们来挑战第四关,从第三关的结果,...

  • Python Challenge 第3关

    地址:http://www.pythonchallenge.com/pc/def/linkedlist.php

  • Python Challenge 第3关

    第3关 没看懂题目,大概意思是一个小写字母,两边是三个大写字母。页面内没有内容,应该跟上一题一样在源码中。果然,源...

  • The Python Challenge(5)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面源码提示: 再点击页面图片显示: 可知是需要...

  • The Python Challenge(8)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 页面和源码中无任何提示,但图片中有一条很明显的灰度线...

  • The Python Challenge(9)

    问题链接 问题链接如下: 答案链接 答案链接如下: 登陆用户名密码为huge和file。 解题思路 阅读源码有如下...

  • The Python Challenge(2)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 将页面给定的字符串根据给定规则进行替换即可,规则如下...

网友评论

      本文标题:Python Challenge[3]

      本文链接:https://www.haomeiwen.com/subject/cyfmittx.html