码迷,mamicode.com
首页 > 编程语言 > 详细

Python之re模块

时间:2017-05-11 22:29:20      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:class   strong   desc   scn   bad   att   flags   images   bsp   

re是一个使用频率很高的模块。

# python2
# -*- coding: utf-8 -*-

import re

str = ‘ABC\\-001‘
print str

str = r‘ABC\-001‘
print str

if re.match(r‘\w{3}\\\-\d{3}‘, str):
    print ‘good match‘
else:
    print ‘bad match‘

src = ‘hello\nworld‘
print src

if re.match(r‘hello\nworld‘, src):
    print ‘good match‘
else:
    print ‘bad match‘

if re.match(r‘hello.world‘, src):
    print ‘good match‘
else:
    print ‘bad match‘

if re.match(r‘hello.world‘, src, re.S):
    print ‘good match‘
else:
    print ‘bad match‘

src = r‘hello\nworld‘
print src

if re.match(r‘hello\\nworld‘, src):
    print ‘good match‘
else:
    print ‘bad match‘

技术分享

这个例子想说明的是,Python中的字符串如果使用‘r‘前缀,字符串中的内容就是本身,没有转义。

re模块的第一个函数:

re.match(pattern, string, flags=0)

一个常用的flag是:

技术分享

Python之re模块

标签:class   strong   desc   scn   bad   att   flags   images   bsp   

原文地址:http://www.cnblogs.com/gattaca/p/6842778.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!