用于查找文件路径的正则表达式 [英] regex for finding file paths
问题描述
我使用这个 regex(\/.*\.[\w:]+)
来查找所有文件路径和目录.但是在这样的行中 "file path/log/file.txt some lines/log/var/file2.txt"
在同一行中包含两个路径,它不会单独选择路径,相反,它选择整行.如何解决这个问题?
I used this regex(\/.*\.[\w:]+)
to find all file paths and directories. But in a line like this "file path /log/file.txt some lines /log/var/file2.txt"
which contains two paths in the same line , it does not select the paths individually , rather , it selects the whole line. How to solve this?
推荐答案
使用 regex(\/.*?\.[\w:]+)
使正则表达式非贪婪.如果要在同一行中查找多个匹配项,可以使用 re.findall().
Use regex(\/.*?\.[\w:]+)
to make regex non-greedy. If you want to find multiple matches in the same line, you can use re.findall().
更新:使用此代码和提供的示例,我得到:
Update: Using this code and the example provided, I get:
import re
re.findall(r'(\/.*?\.[\w:]+)', "file path /log/file.txt some lines /log/var/file2.txt")
['/log/file.txt', '/log/var/file2.txt']
这篇关于用于查找文件路径的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!