跳到内容

regex-flag-alias (FURB167)

派生自 refurb 代码检查工具。

修复总是可用的。

作用

检查是否使用了正则表达式标志的简写别名(例如,使用 re.I 而不是 re.IGNORECASE)。

为什么这不好?

正则表达式模块为每个标志提供了描述性名称,以及单字母别名。 优先选择描述性名称,因为它们更具可读性和自文档性。

示例

import re

if re.match("^hello", "hello world", re.I):
    ...

建议改为

import re

if re.match("^hello", "hello world", re.IGNORECASE):
    ...