# Author:Adminone
import re
#re.match开头往后匹配
# re.match('^chen','ChenRonghua123')
# res = re.match('^chen','ChenRonghua123')
# res = re.match("^chen\d+","Chen321Ronghua123")
# res = re.match("^.+\d+","Chen321Ronghua123")
# res = re.match("^.+","Chen321Ronghua123")
res = re.match("C.+","Chen321Ronghua123")
# print(res)
# print(res.group())
#re.search 文本匹配包含
# res = re.match("R.+","Chen321Ronghua123")
# res = re.search("R.+","Chen321Ronghua123")
res = re.search("R.+a","Chen321Ronghua123")
res = re.search("R.+a","Chen321Ronghua123a")
#只匹配英文字符,$整个字符串,以匹配到的字符结尾
# res = re.search("R[a-z]+a","Chen321Ronghua123a")
# res = re.search("R[a-z]+a","Chen321RongAhuaRonghua123aRonghua####")
# res = re.search("R[a-zA-Z]+a","Chen321RongAhuaRonghua123aRonghua####")
# res = re.search("R[a-zA-Z]+a","Chen321RongAhuaRonghuaaRonghua####")
# res = re.search("#.+#","1123#hello#")
#'?' 匹配前一个字符1次或0次,从前到后匹配
res = re.search("a?","aalex")
res = re.search("a?","lex")
res = re.search("aal?","aalexaaa")
res = re.search("aal?","alexaaa")
res = re.search("[0-9]{3}","aa1x2a3456aa")
# print(res)
# print(res.group())
#re.findall 把所有匹配到的字符放到以列表中的元素返回
# res = re.findall("[0-9]{1,3}","aa1x2a345aa")
# res = re.search("abc|ABC","ABCBabcCD").group()
# res = re.findall("abc|ABC","ABCBabcCD")
#'(...)' 分组匹配,
res = re.search("(abc){2}a(123|456)c", "abcabca456c").group()
res = re.search("(abc){2}(\|\|=){2}", "abcabc||=||=")
#'\A' 只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的
#'\Z' 匹配字符结尾,同$
#以数字开头,字母结尾
res = re.search("\A[0-9]+[a-z]+\Z","123alexabc")
res = re.search("\A[0-9]+[a-zA-Z]+\Z","123Alexabc")
#'\D' 匹配非数字
res = re.search("\D+","12b3$c-\|na")
#'\w' 匹配[A-Za-z0-9]
res = re.search("\w+","12b3$c-\|na")
#'\W' 匹配非[A-Za-z0-9],匹配特殊字符
res = re.search("\W+","12b3$c-\|na")
#'s' 匹配空白字符、\t、\n、\r
res = re.search("\s+","ab\tc1\n3")
#'(?P<name>...)' 分组匹配,例如:身份证分组
res = re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{8})","371481199306143242").groupdict("city")
print(res)
#传值
res = re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{8})","371481199306143242").group("birthday")
print("birthday: ",res)
#re.splitall 以匹配到的字符当做列表分隔符
res = re.split("[0-9]+","abc12de3f45GH")
print(res)
#re.sub 匹配字符并替换
res = re.sub("[0-9]+","|","abc12de3f45GH")
print(res)
#替换前两个
res = re.sub("[0-9]+","|","abc12de3f45GH",count=2)
print(res)
#反斜杠的困扰
res = re.search(r"\\","agjkealgja\er\dl")
print(res)
#re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法,下同)
res = re.search("[a-zA-Z]+","abcA",flags=re.I)
print(res)
#M(MULTILINE): 多行模式,改变'^'和'$'的行为,多行转换为一行匹配
res = re.search("[a-zA-Z]+d$","abcA\nsecondline\nthird",flags=re.M)
print(res)
#S(DOTALL): 点任意匹配模式,改变'.'的行为
res = re.search(".+","abcA\nsecondline")
print(res)
res = re.search(".+","abcA\nsecondline",flags=re.S)
print(res)