regex for finding file paths

前端 未结 3 1743
孤街浪徒
孤街浪徒 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:17

    Your regex (\/.*\.[\w:]+) uses .* which is greedy and would match [\w:]+ after the last dot in file2.txt. You could use .*? instead.

    But it would also match /log////var////.txt

    As an alternative you might use a repeating non greedy pattern that would match the directory structure (?:/[^/]+)+? followed by a part that matches the filename /\w+\.\w+

    (?:/[^/]+)+?/\w+\.\w+

    import re
    s = "file path /log/file.txt some lines /log/var/file2.txt or /log////var////.txt"
    print(re.findall(r'(?:/[^/]+)+?/\w+\.\w+', s))
    

    That would result in:

    ['/log/file.txt', '/log/var/file2.txt']
    

    Demo

提交回复
热议问题