Clearer code in filter_file.
This commit is contained in:
parent
7b71e6fb5a
commit
20388ece86
@ -63,8 +63,11 @@ def filter_file(regex, repl, *filenames, **kwargs):
|
|||||||
# Allow strings to use \1, \2, etc. for replacement, like sed
|
# Allow strings to use \1, \2, etc. for replacement, like sed
|
||||||
if not callable(repl):
|
if not callable(repl):
|
||||||
unescaped = repl.replace(r'\\', '\\')
|
unescaped = repl.replace(r'\\', '\\')
|
||||||
repl = lambda m: re.sub(
|
def replace_groups_with_groupid(m):
|
||||||
r'\\([0-9])', lambda x: m.group(int(x.group(1))), unescaped)
|
def groupid_to_group(x):
|
||||||
|
return m.group(int(x.group(1)))
|
||||||
|
return re.sub(r'\\([1-9])', groupid_to_group, unescaped)
|
||||||
|
repl = replace_groups_with_groupid
|
||||||
|
|
||||||
if string:
|
if string:
|
||||||
regex = re.escape(regex)
|
regex = re.escape(regex)
|
||||||
|
Loading…
Reference in New Issue
Block a user