#test_RegEX
import re
#matching string
str_a = "cat"
str_b = 'bird'
str_c = "dog is catching cat"
print(str_a in str_c)
print(str_b in str_c)
#True
#False
#regular expression
str_a = "cat"
str_b = 'bird'
str_c = "dog is catching cat"
print(re.search(str_a,str_c))
print(re.search(str_b,str_c))
#<re.Match object; span=(6, 9), match='cat'>
#None
#匹配多种可能
str_d = r'd[oa]g'
print(re.search(str_d,str_c))
#<re.Match object; span=(0, 3), match='dog'>
#匹配更多种可能
print(re.search(r"r[A-Z]n",'dog runs to cat'))
#None
print(re.search(r"r[a-z]n",'dog runs to cat'))
#<re.Match object; span=(4, 7), match='run'>
print(re.search(r"r[0-9]n",'dog r2ns to cat'))
#<re.Match object; span=(4, 7), match='r2n'>
print(re.search(r"r[0-9a-z]n",'dog runs to cat'),'\n')
#<re.Match object; span=(4, 7), match='run'>
#特殊种类类匹配
#数字
#\d 所有数字
#\D 所有非数字
print(re.search(r'r\dn','run r4n'))
#<re.Match object; span=(4, 7), match='r4n'>
print(re.search(r'r\Dn','run r4n'),'\n')
#<re.Match object; span=(0, 3), match='run'>
#空白符
#\s 所有空白符 [\t \n \r \f \v]
#\S 相反
print(re.search(r'r\sn','r\nn r4n'))
#<re.Match object; span=(0, 3), match='r\nn'>
print(re.search(r'r\Sn','r\nn r4n'),'\n')
#<re.Match object; span=(4, 7), match='r4n'>
#\w 所有字母数字 ”_“
#\W 相反
print(re.search(r'r\wn','r\nn r4n'))
#<re.Match object; span=(4, 7), match='r4n'>
print(re.search(r'r\Wn','r\nn r4n'),'\n')
#<re.Match object; span=(0, 3), match='r\nn'>
#空白字符
#\b 所有空白符,只有在单词开始和结尾
#\B 空白符,不管单词开始结束都可匹配
print(re.search(r'\bruns\b','dog runs to cat'))
print(re.search(r'\B runs \B','dog runs to cat'),'\n')
# <re.Match object; span=(4, 8), match='runs'>
# <re.Match object; span=(7, 13), match=' runs '>
#\\ 匹配\
#. 匹配任何 除了\n 通配符
print(re.search(r'runs\\','dog runs\ to cat'))
print(re.search(r'r.n','dog r@ns\ to cat'),'\n')
# <re.Match object; span=(4, 9), match='runs\\'>
# <re.Match object; span=(4, 7), match='r@n'>
#^ 句首
#$ 句尾
print(re.search(r'^dog','dog runs\ to cat'))
print(re.search(r'c.t$','dog r@ns\ to cat'),'\n')
# <re.Match object; span=(0, 3), match='dog'>
# <re.Match object; span=(13, 16), match='cat'>
#?是否出现
print(re.search(r'Mon(day)?','Monday'))
print(re.search(r'Mon(day)?','Mon'),'\n')
# <re.Match object; span=(0, 6), match='Monday'>
# <re.Match object; span=(0, 3), match='Mon'>
#多行匹配
string = """
sfsf
I sff
"""
print(re.search(r'^I',string))
print(re.search(r'^I',string,flags=re.M),'\n')
# None
# <re.Match object; span=(6, 7), match='I'>
#* 0次或者多次
#+ 1次或者多次
print(re.search(r'ab*','a'))
print(re.search(r'ab*','abbbbbb'))
# <re.Match object; span=(0, 1), match='a'>
# <re.Match object; span=(0, 7), match='abbbbbb'>
print(re.search(r'ab+','a'))
print(re.search(r'ab+','abbbbbb'),'\n')
# None
# <re.Match object; span=(0, 7), match='abbbbbb'>
#可选次数
print(re.search(r'ab{2,10}','a'))
print(re.search(r'ab{2,10}','abbbbbbb'),'\n')
# None
# <re.Match object; span=(0, 8), match='abbbbbbb'>
#group
match = re.search(r'(\d+), Date:(.+)','ID: 021523, Date:Feb/12/2017') #必须保证前后的格式一致'(\d+), Date: (.+)','ID: 021523, Date:Feb/12/2017'
print(match.group())
print(match.group(1))
print(match.group(2),'\n')
#021523, Date:Feb/12/2017
# 021523
# Feb/12/2017
match = re.search(r'(?P<id>\d+), Date:(?P<data>.+)','ID: 021523, Date:Feb/12/2017') #必须保证前后的格式一致'(\d+), Date: (.+)','ID: 021523, Date:Feb/12/2017'
print(match.group())
print(match.group('id'))
print(match.group('data'),'\n')
#寻找所有匹配
#findall()
#| 或者
print(re.findall(r'r[au]n','ran run ren'))
print(re.findall(r'r(a|u)n','ran run ren'),'\n')
#替换
#re.sub()
print(re.sub(r'r[au]n','catches','dogs run to cat'),'\n')
# dogs catches to cat
#分裂
#re.split()
print(re.split(r"[,;\.]",'a:b,c\d.e'),'\n')
# ['a:b', 'c\\d', 'e']
#compile
compile_re = re.compile(r'r[ua]n')
print(compile_re.search('dog run to cat'))
#<re.Match object; span=(4, 7), match='run'>