#re模块: ''' match 从开头匹配一次 search 只匹配一次 findall 查找所有 sub(正则表达式,'新内容',string) 替换 split result = re.split(r'[,:]','java:99,python:95') 在字符串中搜索如果遇到:或者,就分割 将分割的内容都保存到列表中了 ''' ''' 总结: . 任意字符除(\n) ^ 开头 $ 结尾 [] 范围 [abc] [a-z] [a-z*&¥] 正则预定义: \s 空白 (空格) \b 边界 \d 数字 \w word [0-9a-zA-Z_] 大写反面 \S 非空格 \D 非数字 。。。 '\w[0-9]' ---> \w [0-9] 只能匹配一个字母 量词: * >=0 + >=1 ? 0,1 手机号码正则 re.match('1[35789]\d{9}$',phone) {m} : 固定m位 {m,} >=m {m,n} phone > =m phone<=n ''' import re class huoqu(): def get(self): file = open(r'C:\Users\86159\Desktop\1.txt') relust=file.read() relust1 = re.match('b', relust) print(relust1) def research(self): file = open(r'C:\Users\86159\Desktop\1.txt') relust=file.read() relust1=re.search('b',relust)#查询所在的位置 print(relust1) def spand(self): file = open(r'C:\Users\86159\Desktop\1.txt') relust = file.read() relust1=re.search('b',relust) def findall(self): file = open(r'C:\Users\86159\Desktop\1.txt') relust = file.read() relust1=re.findall('b',relust) relust2=re.findall('[a-z A-z]',relust) print(relust1) print(relust2) def search(self): file = open(r'C:\Users\86159\Desktop\1.txt') relust = file.read() relust1=re.search('[a-z][0-9]',relust)#查询的两个字第一个为a-z,第二个为0-9,并且标注位置 result2 = re.search('^[a-zA-Z]\w{5,}$', relust)# 用户名可以是字母或者数字_, 不能是数字开头,用户名长度必须6位以上 [0-9a-zA-Z] relust3=re.search('[0-9 ]\w{0,}$',relust) print(relust1) print(result2) print(relust3) def group(self): # 爬虫 phone = '010-12345678' result = re.match(r'(\d{3}|\d{4})-(\d{8})$', phone) print(result) # 分别提取 print(result.group()) # () 表示分组 group(1) 表示提取到第一组的内容 group(2)表示第二组的内容 print(result.group(1)) print(result.group(2)) if __name__ == '__main__': a=huoqu() a.get() a.research() a.spand() a.findall() a.search() a.group() email = '123456@qq.com' result = re.match(r'\w{5,20}@(11|136|qq)\.(com|cn)$', email)#以什么头再者以什么结尾\w为长度范围 print(result)
import re import requests import re line =" i am the best hh"#字符串 matchline=re.match(r'(.*) i (.*?) .*',line,re.M|re.I)#从头开始扫一遍re.M\re.I就是多行匹配,影响 ^ 和 $使匹配对大小写不敏感,且以 是 将其分为两组 if matchline: print( "matchObj.group() : ", matchline.group() )#如果当满足match时输出 print ("matchObj.group(1) : ", matchline.group(1) ) print ("matchObj.group(2) : ", matchline.group(2) ) else:#如果没匹配到的 print ("No match!!") line1 = "Cats are smarter than dogs" matchObj = re.match(r'(.*) are (.*?) .*', line1, re.M | re.I) if matchObj: print("matchObj.group() : ", matchObj.group()) print("matchObj.group(1) : ", matchObj.group(1)) print("matchObj.group(2) : ", matchObj.group(2)) else: print("No match!!") import re pattern = re.compile(r'\d+') # 查找数字 result1 = pattern.findall('1ada156')#查询全部 result2 = pattern.findall('123dasd15234', 0, 10)#查询索引0-10的个数 print(result1) print(result2)
it = re.finditer(r"\d+", "12a32bc43jf3") for match in it: print(match.group()) a='12da132' result3=re.finditer(r"\d+",a)#用法大致与findall相同 for match in result3: print(match.group()) result4 = 'aaa bbb ccc;ddd eee,fff' result4=re.split(r';',result4)#split切割以;为原则进行切割 print(result4)