正则表达式在文本中查找目录

时间:2015-03-18 21:57:16

标签: regex path

如何在文本块中找到路径和文件名?

在将此标记为重复之前,我知道存在有关文件路径的问题

例如

In file included from /some/directoy/3.33A.37.2/something else/dogs.txt,
                 from /some/directoy/something else/dogs.txt,
                 from /some/directoyr/3.33A.37.2/something else/dogs.txt,
                 from /var/log/xyz/10032008.log,
                 from /var/log/xyz/test.c:29:
Solution:
please the file something.h has to be alone without others include, it has to be present in release letter, 
in order to be included in /var/log/xyz/test.c and /var/log/xyz/test.h automatically
Other Note: 
The file something.c must contain the somethinge.h and not the ecpfmbsd.h because it doesn't contain C operative code.. everything good.. 

以下是理想的匹配:

/some/directoy/3.33A.37.2/something else/dogs.txt
/some/directoy/something else/dogs.txt
/some/directoyr/3.33A.37.2/something else/dogs.txt
/var/log/xyz/10032008.log
/var/log/xyz/test.c:29 (this is a tricky one, ok with out it)
/var/log/xyz/test.c
/var/log/xyz/test.h

如果我找到答案,我会如何更改它以使用\代替/目录

2 个答案:

答案 0 :(得分:4)

你可以使用这样的正则表达式:

\/.*\.[\w:]+

<强> Working demo

enter image description here

顺便说一句,如果你想在路径中允许反斜杠:

[\\\/].*\.[\w:]+

答案 1 :(得分:2)

这看起来有效:

\/[^,:]*\.\w+

请参阅demo

如果您知道确切的扩展名,长度以及它们的字符,您可以对此进行微调。至于我,\w+会匹配扩展名。