karthik A
karthik A

Reputation: 655

How do I match a unix absolute path and extract it using perl?

I know regular expressions can be used, but am not able to find the right one. Also are there any built-in functions available that do this?

Upvotes: 0

Views: 1986

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 755074

Given:

bmake: stopped in /bb/cc/xx/yy/zz/aa

This regex will pull the pathname:

m%\s(/.*)%

It looks for a white space character followed by a slash followed by anything. If you don't have white space in your path names, then you can use the more restrictive:

m%\s(/\S*)%

If you're sure you'll always have one or more path components to the names, you can add more restrictions:

m%\s(/\S+/\S*)%

And so it goes on. The more you know about what can be valid in the path, the better your chances of matching only the file name. But note that a file name on Unix can contain any character except / (because it is the delimiter between sections of the path name) and \0, the NUL byte. Everything else - newlines, tabs, controls, etc - is fair game and could be part of a file name. Mercifully, most of them usually aren't present in file names.

Note that relative pathnames are even harder than absolute path names.

Upvotes: 2

raina77ow
raina77ow

Reputation: 106483

I'd suggest using File::Spec core module instead. If you just need to check whether what's given to you is absolute path or not, use file_name_is_absolute(); if you need to transform relative path to absolute, use rel2abs(), you see the pattern. ) It's easier and way more readable.

Upvotes: 4

shift66
shift66

Reputation: 11958

/(.+/)*.* use this pattern. A slash at the start, then directories (may not be any) and a file name or directory name in the end (this may not be too). Actually this will match everything which starts with slash but it's OK because path in unix can contain everything except \0.

Upvotes: 1

Related Questions