# import re
#
# ret = re.match("wwo","two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
"""
. 匹配任意1个字符(除了
)
[ ] 匹配[ ]中列举的字符
d 匹配数字,即0-9
D 匹配非数字,即不是数字
s 匹配空白,即 空格
S 匹配非空白
w 匹配单词字符,即a-z、A-Z、0-9、_
W 匹配非单词字符
"""
# import re
#
# ret = re.match(".wo","two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# [ ] 匹配[ ]中列举的字符
# import re
#
# ret = re.match("[art]wo","two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# d 匹配数字,即0-9
# import re
#
# ret = re.match("d[art]wo","9two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# D 匹配非数字,即不是数字
# import re
#
# ret = re.match("D[art]wo","Atwo")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# s 匹配空白,即 空格
# import re
#
# ret = re.match("s[art]wo"," two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# S 匹配非空白
# import re
#
# ret = re.match("S[art]wo","8two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
"""
w 匹配单词字符,即a-z、A-Z、0-9、_
W 匹配非单词字符
"""
# import re
#
# ret = re.match("w[art]wo","#two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# import re
#
# ret = re.match("W[art]wo","_two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
"""
* 匹配前一个字符出现0次或者无限次,即可有可无
+ 匹配前一个字符出现1次或者无限次,即至少有1次
? 匹配前一个字符出现1次或者0次,即要么有1次,要么没有
{m} 匹配前一个字符出现m次
{m,n} 匹配前一个字符出现从m到n次
"""
# import re
#
# ret = re.match("t{,10}wo","ttttttttttttttttttttttttttttttttttttwo")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# import re
#
# ret = re.match("t{5}wo","ttttwo")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# import re
#
# ret = re.match("t?wo","ttttwo")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# import re
#
# ret = re.match("t+wo","two")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
# import re
#
# ret = re.match("t*wo","ttttttttwo")
# if ret:
# print(ret.group())
# else:
# print("匹配失败")
"""
^ 匹配字符串开头
$ 匹配字符串结尾
"""
# import re
# match_ret = re.match("^1[3-9]d{8}[^47]$","15233620504")
#
# if match_ret:
# print(match_ret.group())
# else:
# print("匹配失败")
# import re
# match_ret = re.match("^1[3-9]d{8}[0-35-68-9]$","15233620508")
#
# if match_ret:
# print(match_ret.group())
# else:
# print("匹配失败")
# import re
# match_ret = re.match("^15233620509","15233620509")
#
# if match_ret:
# print(match_ret.group())
# else:
# print("匹配失败")
"""
| 匹配左右任意一个表达式
(ab) 将括号中字符作为一个分组
um 引用分组num匹配到的字符串
(?P<name>) 分组起别名
(?P=name) 引用别名为name分组匹配到的字符串
"""
# laowangaimeinv@163.com
import re
ret = re.match("w{4,20}@(163|126)(.com)$","laowangaimeinv@126.com")
if ret:
print(ret.group())
else:
print("匹配失败")
# <html>hh</html>
#
um 引用分组num匹配到的字符串
# match_ret = re.match(r"<([a-zA-Z]+)>w+</1>","<html>hh</html>")
#
# if match_ret:
# print(match_ret.group())
# else:
# print("匹配失败")
# <html><h1>hh</h1></html>
# match_ret = re.match(r"<([a-zA-Z]+)><([a-zA-Z0-9]+)>w+</2></1>","<html><h1>hh</h1></html>")
#
# if match_ret:
# print(match_ret.group())
# else:
# print("匹配失败")
# (?P<name>) 分组起别名
# (?P=name) 引用别名为name分组匹配到的字符串
# match_ret = re.match(r"<(?P<name1>[a-zA-Z]+)><(?P<name2>[a-zA-Z0-9]+)>w+</(?P=name2)></(?P=name1)>","<html><h1>hh</h1></html>")
#
# if match_ret:
# print(match_ret.group())
# else:
# print("匹配失败")
# (?P<name>) 分组起别名
# (?P=name) 引用别名为name分组匹配到的字符串
# r的使用建议
"""
Python中字符串前面加上 r 表示原生字符串,
与大多数编程语言相同,正则表达式里使用""作为转义字符,这就可能造成反斜杠困扰。
假如你需要匹配文本中的字符"",那么使用编程语言表示的正则表达式里将需要4个反斜杠"\\":
前两个和后两个分别用于在编程语言里转义成反斜杠,转换成两个反斜杠后再在正则表达式里转义成一个反斜杠。
Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r"\"表示。同样,匹配一个数字的"\d"可以写成r"d"。
有了原生字符串,你再也不用担心是不是漏写了反斜杠,写出来的表达式也更直观
"""