正则是一个十分重要且基础的模块
学习正则模块就要了解正则的一些基本字符
正则的基本方法有很多
但是大体上分为三种匹配 分割 替换
匹配有match search fullmatch findall finditer
注意finditer得到结果是一个可迭代类型需要遍历才能得到结果
使用group方法就可以查看返回的内容
import re
result = re.match(r"abc","abcabcd")
print(result,result.group())
result = re.search(r"abc","abcabcdabcde")
print(result,result.group())
result = re.fullmatch(r"a\w+c","abcabcdabcdec")
print(result,result.group())
result = re.findall(r"abc","abcabcdabcde")
print(result)
result = re.finditer(r"abc","abcabcdabcde")
for i in result:
print(i,i.group())
print(result)
替换 sub subn
这两个方法还有一个参数为新的查找内容
result = re.sub(r"abc","你好","abcabcdabcde")
print(result)
result = re.subn(r"abc","你好","abcabcdabcde")
print(result)
分割 split 使用默认切割所有 maxsplit 控制切割次数
result = re.split(r"abc","abcabcddabcde",maxsplit=1)
print(result)
以下便是使用正则字符进行查找
import re
result = re.findall(r"\d+?", "123456789")
print(result)
result = re.findall(r"\d+", "123456789")
print(result)
result = re.findall(r"\d*", "123456789")
print(result)
result = re.findall(r"a\w+7", "a123456789c")
print(result)
result = re.findall(r"\w{2,5}","acsvddffdsfz13156446FSADFAFSF")
print(result)
result = re.findall(r"\w{10}","acsvddffdsf13156446FSADFAFSF")
print(result)
result = re.findall(r"a(.*)d(.*)F","acsvddffdsf13156446FSADFAFSF")
print(result)
result = re.findall(r"[a-z]|[0-9]|[A-Z]","acsvddffdsf13156446FSADFAFSF")
print(result)