python例子三

例一:匹配长度为1-15的域名

#-*-encoding:utf-8-*-

import re



regex=re.compile('^www[.][a-z]{1,15}[.](com|org)')

m1=re.match(regex, 'www.baidu.com')

#m2=re.match(regex, 'abcdefghig')



if m1 is not None:

    print 'true1'

#if m2 is not None:

 #   print 'true2'

 例二:匹配010 022之类的电话号码

#-*-encoding:utf-8-*-

import re



regex = re.compile('\(?0\d{2}[)-]?\d{8}')



m1= re.search(regex, '(010)8886666')

m2=re.search(regex, '022-22334455')

m3=re.search(regex, '02912345678')

m4= re.search(regex, '(010)88886666 022-22334455 02912345678')



if m4 is not None:

    print m4.group()

m5 = re.findall(regex,'(010)88886666 022-2233s4455 02912345678')

print m5

    

 

你可能感兴趣的:(python)