#! /usr/bin/python3
# -*-coding:UTF-8-*-
import re
print(re.match("hello","hello my world").span()) #在起始位置匹配,只匹配字符串开始部分,失败就返回None
print(re.match("hi","hi 你好"))
print(re.search("apple","banana orange apple").span()) #随意位置匹配,直到找到匹配对象,否则没有返回None
print(re.search("banana","banana orange apple").span()) #在起始位置匹配
#由于正则会消耗一定的资源,所以要用预编译
re_phone = re.compile(r‘^(\d{3})-(\d{3,8})$‘)
print(re_phone.search(‘020-12345678‘).group())
原文地址:http://blog.51cto.com/3miao/2086222