# 正则表达式 用来处理字符串,查找替换# match 和 search 方法的执行结果是一个Match类型的对象# 在正则表达式里,匹配1个 '\' ,需要使用4个 '\\\\''# 第一个参数是正则匹配规则,第二个参数表示需要匹配的字符串import re
x ='hello\\nworld'
m = re.search('\\\\',x)# 前面可以加一个 r 表示原生字符
m = re.search(r'\\',x)print(m)
'''
查找相关的方法
match: 查找字符串,返回的结果是一个 re.Match 对象
search:查找字符串,返回的结果是一个 re.Match 对象
相同点:1.只对字符串查询一次 2.返回值类型都是 re.Match 类型的对象
不同点:1. match 是从头开始匹配,一旦匹配失败,就返回 None
2. search 是在整个字符串里匹配
finditer:查找所有的匹配数据放到一个可迭代对象里
findall:把查找到的所有字符串结果放到一个列表里
fullmatch:完全匹配,字符串需要完全满足正则规则才会有结果,否则就是 None
'''import re
from collections.abc import Iterable
x1 = re.search('hello','hello world')print(x1)
x2 = re.match('hello','hello world')print(x2)
x3 = re.match('hello','world hello')print(x3)print(re.search('x','zh32jkahdjc3x3b4x45wkdx5odik3ohjx'))
a = re.finditer('x','zh32jkahdjc3x3b4x45wkdx5odik3ohjx')print(isinstance(a,Iterable))for b in a:print(b)
c = re.findall('x','zh32jkahdjc3x3b4x45wkdx5odik3ohjx')
c2 = re.findall('x\d+','zh32jkahdjc3x3b4x45wkdx5odik3ohjx')print(c)print(c2)
e = re.fullmatch('hello python','hello python')
f = re.fullmatch('h.*d','hello python world')print(e)print(f)