可以使用re模块的findall函数来实现这个功能。下面是一个示例代码:

import re

text = "gallery_image_1 gallery_image_2 gallery_image_3 gallery_image_4"

pattern = r"gallery_image_(\d+)"
result = re.findall(pattern, text)

print(result)

输出结果为:

['1', '2', '3', '4']

在上述代码中,首先定义了一个正则表达式模式gallery_image_(\d+),其中\d+表示匹配一个或多个数字。然后使用re模块的findall函数在text中查找所有满足该模式的字符串。最后将匹配结果保存在result数组中,并打印输出。

python使用正则表达式匹配所有gallery_image_xx中的文字其中xx为数字并将匹配结果保存至一个数组中

原文地址: http://www.cveoy.top/t/topic/h183 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录