为什么不能将后缀全部匹配出来
代码见下:import re
str1 =('C:/Users/a/Desktop/pic/12.bmp', 'C:/Users/a/Desktop/pic/15.bmp', \
'C:/Users/a/Desktop/pic/21.png', 'C:/Users/a/Desktop/pic/31.jpg')
# a = re.findall("匹配规则", "这个字符串是否有匹配规则的字符")
pattern = re.findall(r'-zC-Z]:/[\w\-/]*.[(bmp)|(jpg)|(png)]',str(str1))
print(pattern)
print(type(pattern))
for i in pattern:
print(i,end='')
结果如下:
C:\Users\a\Desktop\新建文件夹\venv\Scripts\python.exe C:/Users/a/PycharmProjects/pythonProject6/shiya-001
['C:/Users/a/Desktop/pic/12.b', 'C:/Users/a/Desktop/pic/15.b', 'C:/Users/a/Desktop/pic/21.p', 'C:/Users/a/Desktop/pic/31.j']
<class 'list'>
C:/Users/a/Desktop/pic/12.bC:/Users/a/Desktop/pic/15.bC:/Users/a/Desktop/pic/21.pC:/Users/a/Desktop/pic/31.j
Process finished with exit code 0
问题是:为什么我不能将文件名后缀完全用正则表达式匹配起来,正确的正则的表达式是怎么写的(将文件的路径及文件名全部提取出来.
import re
s = '''C:/Users/a/Desktop/pic/12.bmp
C:/Users/a/Desktop/pic/15.bmp
C:/Users/a/Desktop/pic/21.png
C:/Users/a/Desktop/pic/31.jpg'''
result = re.findall(r':/[\w\-/]*\.(?:bmp|jpg|png)', s)
print(result)
# 如果你是为了搜索图片,可以用os.walk
# import os
#
# for d, _, fs in os.walk('.'):
# for f in fs:
# if f[-3:] in ['jpg', 'png', 'bmp']:
# print(f'{d}/{f}')
我实际是要匹配通过tkinter.Entry获取的get值,那段变量是模拟的值,而不是只要区取图片文件
页:
[1]