# -*- 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