您的位置:首页 > 编程语言 > Python开发

Python RE模块中search()和match()的区别

2015-09-01 21:38 761 查看
# -*- coding: utf-8 -*-

import re

s1 = "helloworld, qwer"
s2 = "hello world, qwer"
w1 = 'hello'
w2 = 'world'
m1 = re.search(w1, s1) # 扫描整个字符串查找匹配
m2 = re.match(w1, s2) # 只在字符串的开始位置匹配
m3 = re.search(w2, s1)
m4 = re.match(w2, s2)

if m1:
print 'm1: ', m1.group()
if m2:
print 'm2: ', m2.group()
if m3:
print 'm3: ', m3.group()
if m4:
print 'm4: ', m4.group()


输出:

m1:  hello

m2:  hello

m3:  world

原文:http://blog.csdn.net/cnmilan/article/details/9071999
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: