Python 多重正则表达式替换
我使用以下代码规范化文件名:Python 多重正则表达式替换,python,regex,Python,Regex,我使用以下代码规范化文件名: new_file = re.sub('[. ]', '_', old_file.lower()) new_file = re.sub('__+', '_', new_file) new_file = re.sub('[][)(}{]', '', new_file) new_file = re.sub('[-_]([^-_]+)$', r'.\1', new_file) 我的问题是,是否有可能以更好的方式编写此代码 我发现: 但此代码仅适用于普通字符串。第3行中
new_file = re.sub('[. ]', '_', old_file.lower())
new_file = re.sub('__+', '_', new_file)
new_file = re.sub('[][)(}{]', '', new_file)
new_file = re.sub('[-_]([^-_]+)$', r'.\1', new_file)
我的问题是,是否有可能以更好的方式编写此代码
我发现:
但此代码仅适用于普通字符串。第3行中的映射(关于escape,…
将“销毁”正则表达式
问候,Ray如果您只是在寻找更易于维护、重复性更少的代码(与算法更改相反),请使用一个简单的
for
循环:
SUBS = [
('[. ]', '_'),
('__+', '_'),
('[][)(}{]', ''),
('[-_]([^-_]+)$', r'.\1'),
]
def normalize(name):
name = name.lower()
for pattern, replacement in SUBS:
name = re.sub(pattern, replacement, name)
return name
您是否尝试过简单地删除
re.escape
,即将有问题的行更改为regex=re.compile((%s)”%“|”。join(dict))
?我没有试过,但我看不出它不起作用的原因。我已经试过了。在本例中,第5行有一个错误。此外,在我的示例中,序列很重要,我发现a字典会弄乱序列。我鼓励OP将('[.]',''''''
和(''.+',''''''''.'
合并到('[.\u]+',“\ u'
这实际上是一样的,不会变成一个非常不可读的正则表达式。@user4815162342:谢谢你的解决方案。我喜欢它,我会在我的代码中使用它。。。
SUBS = [
('[. ]', '_'),
('__+', '_'),
('[][)(}{]', ''),
('[-_]([^-_]+)$', r'.\1'),
]
def normalize(name):
name = name.lower()
for pattern, replacement in SUBS:
name = re.sub(pattern, replacement, name)
return name