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

3

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

在你标记这个问题为重复之前,我知道关于文件路径的问题已经存在。

例如:

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个回答

4

您可以使用此类正则表达式:

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

工作演示

输入图片描述

顺便说一下,如果您想在路径中允许反斜杠,则可以使用:

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

你很快就能做出更改。 - Whitecat
这里有一个问题,如果我有这个 something /var/log/xyz/10032008.log,它必须在发布信中出现,才能自动包含在 /var/log/xyz/test.c:54546 中。 - Whitecat
你可以尝试使用 \/.*\.[\w:\s]+,但这会破坏其他匹配。 - Federico Piazza

2
这似乎正在运作:

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

演示
如果您知道确切的扩展名、它们的长度以及它们包含哪些字符,那么您可以微调它。至于我,\w+ 可以匹配扩展名。

很好,我喜欢这个。它可以处理 \ / 两种情况。我正在解析“大数据”,但我不知道任何扩展名。我希望尽可能通用。 - Whitecat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接