import re
re.match(pat,string)
re.compile()
re.sub(pat,repl,string)
re.findall(pat,text)
一般字符:
. 匹配任意除换行符"\n"外的字符
\ 转义字符  

举例: a\.c   a.c
        a\\c   a\c
[...] 字符集

举例:  a[bcd]e   abe  ace  ade
| 匹配左右表达式任意一个,先左后又 (abc|def) abc  def  
预定义字符:
\d 数字[0-9]
\D 非数字[^\d]
\s 空白字符(空格)
\S 非空格[^\s]
\w 单词字符[A-Za-z0-9]
\W 非单词字符[^\w]
数量词:用在字符后
* 匹配前一个字符0次或无限次
+ 匹配前一个字符一次或无限次
? 匹配前一个字符0次或一次
{m} 匹配前一个字符m次
{m,n} 匹配前一个字符m至n次

附上对于邮箱,电话,手机的匹配小例子:

import re

r1=r"^\w+[-_.]*[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$" r2=r"^1[3578]\d{9}$"

s1="young.yang@platinum.com"

s2="17602108975"

t1=re.findall(r1,s1)

t2=re.findall(r2,s2)

print (t1)

print (t2)

 

posted on 2017-05-17 11:21  不吃西红柿a  阅读(1355)  评论(0编辑  收藏  举报