+ -
当前位置:首页 → 问答吧 → 请教re.findall输出问题

请教re.findall输出问题

时间:2011-07-04

来源:互联网

新人学习python中


手上是python2的书,环境为Python 2.7.1

书上脚本与输出为:
  1. #!/usr/bin/env python
  2. import re
  3. re_string = "{{{.*?}}}"   
  4. some_string = "this is a string with {{words}} embedded in\
  5. {{curly brackets}} to show an {{example}} of {{regular expressions}}"
  6. for match in re.findall(re_string, some_string):
  7.         print "MATCH->", match


  8. $ ./retest1.py
  9. MATCH-> words
  10. MATCH-> curly brackets
  11. MATCH-> example
  12. MATCH-> regular expressions
复制代码
================
实际运行该脚本为无输出
修改后脚本与输出:
  1. #!/usr/bin/env python
  2. import re
  3. re_string = "{{.*?}}"   
  4. some_string = "this is a string with {{words}} embedded in\
  5. {{curly brackets}} to show an {{example}} of {{regular expressions}}"
  6. for match in re.findall(re_string, some_string):
  7.         print "MATCH->", match

  8. $ ./retest1.py
  9. MATCH-> {{words}}
  10. MATCH-> {{curly brackets}}
  11. MATCH-> {{example}}
  12. MATCH-> {{regular expressions}}
复制代码
====
这是版本变化造成还是我写错了?

另外:如果输出时候不需要{{}}将查找结果括起来,是否有简单参数直接去除前后括号?
先谢谢

作者: george_young   发布时间: 2011-07-04

用 re_string = "{{(.*?)}}" 就行了,估计是印刷错误。

作者: pytoon   发布时间: 2011-07-04

收到,感谢

作者: george_young   发布时间: 2011-07-04

热门下载

更多