#! /usr/bin/env python
# coding:utf-8
import re
# lista = re.findall('a.c','abcabc') #点可以匹配除换行以外任意字符
# lista = re.findall('^abc','abcabc') #匹配以什么开头的字符
# lista = re.findall('bc$','abcabc') #匹配以什么结尾的字符
# lista = re.findall('abc*','abccccabcc') #匹配*前面的字符,0个以上
# lista = re.findall('abc+','abccabcc') #匹配*前面的字符,1个以上
# lista = re.findall('abc?','abccabcc') #匹配?前面的字符,0次或者一次
# lista = re.findall('abc{2}','abccccccabcccc')# 匹配{}前面的字符两次
# lista = re.findall('[a-z]','abccccccabcccc') # 匹配a-z 的字符,一个一个全部添加到列表
# lista = re.findall('[0-9]','abccc2cccab3cccc') #匹配0-9,一个一个全部添加到列表
# lista = re.findall('[^a-z]','abccccc3cabc5ccc') #匹配除了 a-z 的其他字符
# lista = re.findall('(?:a|b)\d+','a4b5aabb')
# lista = re.findall('\d+','1h2h3hk4l') # \d 匹配数字
# lista = re.findall('\D+','1h2h3hk4l') # \D 匹配非数字
# lista = re.findall('\s\d', ' 1h 2h 3h k4l') # \s匹配 空格
# lista = re.findall('\S', ' 1h 2h 3h k4l') # 匹配非空格字符
# lista = re.findall('\w', ' 1h_2h*3h&k4l') #匹配字母数字下划线
# lista = re.findall('\W','1h_2h*3h&k4l') #匹配非数字字母下划线字符
# lista = re.findall('abc|efg', ' abcadefgabdcabc') # | 匹配abc或者egf
# lista = re.findall('(?:abc){1,2}', 'abcdjfkabcabcs')# 分组,匹配abc 1到2次
# lista = re.findall('a(?:bc|de)f', 'abcfadef') # 分组匹配abcf,adef
# lista = re.findall(r'(\d)\w+?\1', '1abc1') #分组 前后要一样才可以使用编号
# lista = re.findall(r'(?P<my>[0-9])abc(?P=my)', '1abc1') #这个也是前后一样, 引用别名(?P<my>) 使用别名(?P=my)
# lista = re.findall(r'(?<=[0-9]).*?(?=\d)', '1abc0') # (?<=[0-9]) 指定开头 (?=\d)指定结尾 将中间的值返回到list
# lista = re.findall(r'(?# zheshizhus)\d', '1abc0') # (?# zheshizhus) 这是注释 不执行
# print(lista)
#re.I 忽略大小写
#re.M 多行匹配
#re.S 匹配所有字符 包含换行符
# compile函数 生成一个正则表达式对象
# match函数 从头开始匹配,匹配不成功返回None
# cp = re.compile('(\w+?)(\d+)',re.I)
# matcho = cp.match('abcdef111') #返回一个match对象
# print(matcho.group()) #返回匹配的字符串
# print(matcho.group(1)) #返回第一个组的字符串
# print(matcho.group(2)) #返回第二个组的字符串
# print(matcho.span()) #返回匹配字符串的索引范围
# print(matcho.start()) #返回匹配字符串的开始位置
# print(matcho.end()) #返回匹配字符串的结束位置
#search函数,对整个字符串进行搜索,找到第一个匹配的,返回一个列表
# cp = re.compile('(\w+?)(\d+)',re.M)
# matcho = cp.search('&&&&&ABcdef124334') 返回一个match对象
# print(matcho.group())
# print(matcho.group(2))
#findall函数 返回一个列表
# cp = re.compile('\d')
# lista = cp.findall('one1two2three3four4')
# print(lista)
#finditer函数,返回一个可迭代的 match对象
# cp = re.compile('\d')
# itera = cp.finditer('one1two2three3four4')
# for i in itera:
# print(i.group())
# print(i.span())
#split函数 分割字符串 返回一个列表
# cp = re.compile('\d')
# lista = cp.split('one1two2three3four4')
# print(lista)
#sub 函数,将字符串中的内容按规则匹配 ,后面还可以指定被替换的次数
# res = re.sub('\s+','-','a b c d e f',3)
# print(res)
#subn函数,将字符串按规则进行匹配,返回时用元组返回,其中有被替换的次数
# res = re.subn('\s+','-','a b c d e f',3)
# print(res)