正则表达式 ###################################### search ********************************************* .. code-block:: python scan_res = re.search(name, line) if scan_res != None: state = 1 scan_res.group(num)返回匹配的数据 findall ********************************************* .. code-block:: python name_cfg=r"START(\w*)END" regex_1 = re.compile(name_cfg) res=re.findall(regex_1,line) if res[0]=='8': res 返回找到的括号中的内容 replace ********************************************* .. code-block:: python re_str = r"START(\w*)END" replace_str = re.sub(re_str, r"START"+val+r"END", line) 关键字匹配 ********************************************* `菜鸟教程 Python 正则表达式🐣 `_ ========= ======================================= 关键字 描述 ========= ======================================= \\w 匹配字母数字及下划线 \\W 匹配非字母数字及下划线 \\s 匹配任意空白字符,等价于 [ \t\n\r\f]。 \\S 匹配任意非空字符 \\d 匹配任意数字,等价于 [0-9]. \\D 匹配任意非数字 ========= =======================================