regex for finding file paths

前端 未结 3 1789
孤街浪徒
孤街浪徒 2021-01-19 07:51

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

3条回答
  •  情书的邮戳
    2021-01-19 08:12

    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']
    

提交回复
热议问题