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