Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式组的引用长度_Python_Regex_Python 2.7_Python 3.x - Fatal编程技术网

Python 正则表达式组的引用长度

Python 正则表达式组的引用长度,python,regex,python-2.7,python-3.x,Python,Regex,Python 2.7,Python 3.x,我试图用散列替换文件名末尾的数字,例如image.0010001.tiff将变成image.tiff 是否可以使用单个re.sub方法执行此操作 这就是我到目前为止所做的: re.sub('(\d+)(?=\.\w+$)', '#'*len('\g<1>'), 'image.0010001.tiff') re.sub(“(\d+(=\.\w+$)”、“#”*len(“\g”)、“image.0010001.tiff”) 您可以将函数传递给re.sub,如下所示: re.sub('(

我试图用散列替换文件名末尾的数字,例如image.0010001.tiff将变成image.tiff

是否可以使用单个re.sub方法执行此操作

这就是我到目前为止所做的:

re.sub('(\d+)(?=\.\w+$)', '#'*len('\g<1>'), 'image.0010001.tiff')
re.sub(“(\d+(=\.\w+$)”、“#”*len(“\g”)、“image.0010001.tiff”)

您可以将函数传递给
re.sub
,如下所示:

re.sub('(\d+)(?=\.\w+$)', lambda match:'#'*len(match.group(1)), 'image.0010001.tiff')

不错。谢谢