简体   繁体   中英

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?

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.

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.

/(.+/)*.* 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM