Chinaunix首页 | 论坛 | 博客
  • 博客访问: 461248
  • 博文数量: 108
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-29 19:43
文章分类

全部博文(108)

文章存档

2016年(10)

2015年(9)

2014年(73)

2013年(16)

我的朋友

分类: Python/Ruby

2014-08-14 18:28:03

python  re group用法

点击(此处)折叠或打开

  1. >>> p=re.compile('(a)b')
  2. >>> m=p.match('ab')
  3. >>> m.group()
  4. 'ab'
  5. >>> m.group(0)
  6. 'ab'
  7. >>> m.group(1)
  8. 'a'
  9. >>> m.group(2)
  10. Traceback (most recent call last):
  11.   File "", line 1, in <module>
  12. IndexError: no such group
  13. >>> p=re.compile('(a(b)c)d')
  14. >>> m=p.match('abcd')
  15. >>> m.group()
  16. 'abcd'
  17. >>> m.group(0)
  18. 'abcd'
  19. >>> m.group(1)
  20. 'abc'
  21. >>> m.group(2)
  22. 'b'
  23. >>> m.group(3)
  24. Traceback (most recent call last):
  25.   File "", line 1, in <module>
  26. IndexError: no such group
  27. >>>


点击(此处)折叠或打开

  1. Python 2.7.6 (default, Mar 22 2014, 22:59:56)
  2. [GCC 4.8.2] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import re
  5. >>> re.split('\W+','Words, words, words')
  6. ['Words', 'words', 'words']
  7. >>> re.split('(\W+)','Words, words, words')
  8. ['Words', ', ', 'words', ', ', 'words']
  9. >>> re.split('\W+','Words, words, words',1)
  10. ['Words', 'words, words']
  11. >>> re.split('[a-f]+','0a3B9',flags=re.IGNORECASE)
  12. ['0', '3', '9']

阅读(1034) | 评论(0) | 转发(0) |
0

上一篇:linux bc 用法

下一篇:redis php

给主人留下些什么吧!~~