2015-03-18 59 views
0

如何在文本塊中查找路徑和文件名?正則表達式在文本中查找目錄

您標記這是重複之前,我知道有關文件路徑的問題存在

例如

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

您可以使用正則表達式是這樣的:

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

Working demo

enter image description here

順便說一句,如果你想允許路徑反斜槓,你可以有:

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

您可以快速做出更改。 – Whitecat 2015-03-18 22:03:28

+1

@Whitecat大聲笑,我喜歡正則表達式和發現模式 – 2015-03-18 22:08:23

+0

這有一個問題,如果我有這個'something /var/log/xyz/10032008.log lude,它必須出現在發佈信中,才能包含在/var/log/xyz/test.c:54546自動' – Whitecat 2015-03-18 22:20:29

1

這看起來工作:

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

請參閱demo

如果您知道確切的擴展名,它們的長度以及它們具有的字符,則可以對其進行微調。至於我,\w+會匹配擴展名。

+0

不錯,我喜歡這個。它同時具有'\ /'。我解析「大數據」我不知道任何擴展。我希望儘可能使這一點儘可能通用。 – Whitecat 2015-03-18 22:05:50

相關問題