Whitecat
Whitecat

Reputation: 4020

Regex to find directory in text

How do I find a path and file name in a block of text?

Before you mark this as duplicate I know questions about file paths exist

For example

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.. 

The following are the ideal matches:

/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

Going further what if I find an answer how can I change it to work with \ instead of / directories

Upvotes: 3

Views: 11676

Answers (2)

Federico Piazza
Federico Piazza

Reputation: 30985

You can use a regex like this:

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

Working demo

enter image description here

Btw, if you want to allow backslashes in the path you can have:

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

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

This looks to be working:

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

See demo.

You can fine-tune this if you know the exact extensions, their lengths and what characters they have. As for me, \w+ would do to match extensions.

Upvotes: 2

Related Questions