#! /usr/bin/env python# -*- coding=utf-8 -*-import retext = "pythontab"m = re.match(r"w+", text)if m: print m.group(0)else:print "not match"结果是:pythontab
#! /usr/bin/env python# -*- coding=utf-8 -*-#import retext = "@pythontab"m = re.match(r"w+", text)if m: print m.group(0)else:print "not match"结果是:not match
#! /usr/bin/env python# -*- coding=utf-8 -*-#import retext = "pythontab"m = re.search(r"w+", text)if m: print m.group(0)else:print "not match"结果是:pythontab
#! /usr/bin/env python# -*- coding=utf-8 -*-#import retext = "@pythontab"m = re.search(r"w+", text)if m: print m.group(0)else:print "not match"结果是:pythontab
Python中正则表达式search()函数
search函数和match函数有点类似,都可以匹配模式,但是match和search函数也有区别,而且区别很大,match函数只能够字符串的开始位置开始匹配,而search是可以匹配字符串的任意位置,但也是返回找到的第一个匹配的模式。我们通过例子来了解这俩之间的区别吧。