Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What would be the correct perl regex for detecting only Unix paths?

./dir1/dir2/file.ext - invalid
./dir1/dir2/ - valid
./dir1/dir2 - valid
share|improve this question
4  
Why is the first path invalid? Dot is legal in directory name. –  choroba May 16 '13 at 22:52
    
Those are all valid paths. Also multiple slashes like ///dir1/dir2//dir3 are valid. –  Matt H May 16 '13 at 22:53
2  
What are you really trying to do? Trying to detect directories with a regex is almost certainly the wrong solution. You are usually better off just testing to see if the path exists. stackoverflow.com/questions/206320/… –  Zoredache May 16 '13 at 23:15
    
@choroba and Matt H I was not aware of that. Good to know. Zoredache I was trying to use regex to omit unix paths for Google Search Appliance (GSA), since it accepts regex. I'll stick with simple specific paths then. –  slashoofpez May 17 '13 at 17:08
add comment

2 Answers 2

There is none.

There's no way to distinguish whether a basename is a file or directory by looking at the path alone.

share|improve this answer
add comment

Do man perlfunc and look at the first set of functions under Alphabetical Listing of Perl Functions. There are a set of functions, beginning with -, that test what type of file/whatever a path refers to. The one you probably want is -d, which tests if a file refers to a directory.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.