# -*- coding:utf-8 -*-
# .:代表任意一个字符(除换行符"\n")
# \:转义符 \s空格
# +:将前面一个字符或一个表达式重复1遍或多遍
# *: 将前面一个字符或一个表达式重复0遍或多遍
# []:表示匹配中括号内字符中的任意一个
# [^]:表示除了中括号内的字符,都匹配
# [0-9]、[a-z]、[A-Z]、\d (等同于[0-9])、\D (等同于[^0-9]匹配非数字)、\w (等同于[a-z0-9A-Z_]匹配大小写字母、数字和下划线)、\W (等同于[^a-z0-9A-Z_],即等同于上一条取非)
# ?:匹配前一个字符或子表达式0次或1次重复
# +? *?:将贪婪的“+/*”改成了懒惰的“+/*”
# {a,b}:表示a<=匹配次数<=b
# ^ \A:匹配字符串的开头 (^: 在多行模式中匹配每一行的开头)
# $ \Z:匹配字符串结束 ($:在多行模式中匹配每一行的末尾)
'''
compile()、match()、search()
findall()、finditer()
split()
sub()、subn()
'''
import re
key1 = r'<html><body><h1>hello world<h1></body></html>'
p1 = r'(?<=<h1>).+?(?=<h1>)'
pattern1 = re.compile(p1)
matcher1 = re.search(pattern1, key1)
print(matcher1.group(0))
key2 = r"javapythonhtmlvhdl"
p2 = r'python'
pattern2 = re.compile(p2)
matcher2 = re.search(pattern2, key2)
print(matcher2.group(0))
key3 = r"<h1>hello world<h1>"
p3 = r'<h1>.+<h1>'
pattern3 = re.compile(p3)
print(pattern3.findall(key3))
key4 = r"afiouwehrfuichuxiuhong@hit.edu.cnaskdjhfiosueh"
p4 = r'chuxiuhong@hit\.edu\.cn'
pattern4 = re.compile(p4)
print(pattern4.findall(key4))
key5 = r"http://www.nsfbuhwe.com and https://www.auhfisna.com"
p5 = r'https*://'
pattern5 = re.compile(p5)
print(pattern5.findall(key5))
key6 = r"lalala<hTml>hello</Html>heiheihei"
p6 = r'<[hH][tT][mM][lL]>.+?</[hH][tT][mM][lL]>'
pattern6 = re.compile(p6)
print(pattern6.findall(key6))
key7 = r'mat cat hat pat'
p7 = r'[^p]at'
pattern7 = re.compile(p7)
print(pattern7.findall(key7))
key8 = r"chuxiuhong@hit.edu.cn"
p8 = r'@.+?\.'
pattern8 = re.compile(p8)
print(pattern8.findall(key8))
key9 = r'saas and sas and saaas ss'
p9 = r'\Asa{,2}s|sa{,2}s$'
pattern9 = re.compile(p9)
print(pattern9.findall(key9))
key10 = r'Tina is a good girl, she is cool, clever, and so on...ooh'
pattern10 = re.compile(r'\w*oo\w')
print(pattern10.findall(key10))
print(re.match('com', 'comwww.runcomoob').group())
print(re.match('com', 'Comwww.runcomoob', re.I).group())
print(re.search('\dcom', 'www.4Comrunoob.5com', re.I).group())
key11 = r'123abc456'
p11 = r'([0-9]*)([a-z]*)([0-9]*)'
s11 = re.search(p11, key11)
print('')
print(s11.group())
print(s11.group(0))
print(s11.group(1))
print(s11.group(2))
print(s11.group(3))
print(s11.groups())
key12 = r'o1n2m3k4'
p12 = r'\d+'
pattern12 = re.compile(p12)
print(pattern12.findall(key12))
key13 = "Tina is a good girl, she is cOol, clever, and so on...ooh"
p13 = r'\w*oo\w*'
pattern13 = re.compile(p13)
print('')
print(pattern13.findall(key13))
print(re.findall(p13, key13, re.I))
print(re.findall(r'(\w)*oo(\w)*', key13)) #()表示子表达式
iter = re.finditer(r'\d+', '12 drumm44ers drumming, 11 ... 10 ...')
for i in iter:
print(i)
print(i.group())
print(i.span())
print(re.split(r'\d+', 'one1Two2three3four4five5', maxsplit=3, flags=re.I))
key14 = 'JGood is a handsome boy, he is cool, clever, and so on...'
print(re.sub(r'\s+', '-', key14, count=2))
print(re.sub(r'\s+', lambda m:'['+m.group(0)+']', key14))
print(re.subn(r'\s+', '-', key14))
pattern15 = re.compile(r'\d{3}-\d{6}')
print(pattern15.findall('010-968888'))
print(re.search(r"(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])", "192.168.1.1").group())
key16 = r'ooo.1on2mo.3ok4'
p16 = r'o\.'
pattern16 = re.compile(p16)
print(pattern16.findall(key16))