所用版本:Python 3.9
1 2 3 4 5 6 7 8 9 10 11 |
# -*- coding: utf-8 -*- import re pattern = re.compile("AA") # 创建模式对象(括号内为正则表达式) match = pattern.search("ABABAA") # 校验字符串(括号内为需要校验的内容) # 返回第一个匹配到的字符串的下标和内容 match = re.search("AA", "ABABAA") # 另一种写法 match = re.findall("AA", "ABABAABAA") # 查找所有符合的字符串 match = re.findall("[A-Z]", "gfhGHHvghH") # 查找所有大写字母 s = re.sub('a', 'A', "szaada") # 替换操作 |