Take the 2-minute tour ×

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?

share|improve this question
Could you explain a little more clearly what you want? Exactly what do you want to match and extract? Can you give some example input strings that you want to match & not match, and what output you're after? –  mathematical.coffee Feb 28 '12 at 6:30
Not without some extra restrictions, they can't. Modern *nix paths can contain any character except \0. Meaning, you can have a path that looks like a sentence if you want. The only thing you can really go by, for absolute pathnames, is a leading slash. –  cHao Feb 28 '12 at 6:30
"bmake: stopped in /bb/cc/xx/yy/zz/aa". from this I just want to extract /bb/cc/xx/yy/zz/aa –  karthik A Feb 28 '12 at 6:35
In this case I'd suggest matching bmake: stopped in instead, and then whatever follows it. Like /stopped in (.*)/. –  raina77ow Feb 28 '12 at 6:52
add comment

3 Answers

up vote 2 down vote accepted



This regex will pull the pathname:


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:


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


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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
In perl i have to do this right ? $line=~m/ regex/ ?? –  karthik A Feb 28 '12 at 6:37
yes.see this troubleshooters.com/codecorn/littperl/perlreg.htm –  shift66 Feb 28 '12 at 6:38
if($line =~ /[/](.+[/])*.*/ ) , so is this correct? –  karthik A Feb 28 '12 at 6:43
yes, I guess.try and tell if it doesn't work –  shift66 Feb 28 '12 at 6:44
\Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE / at test.pl line 26.. this is what I get ? Is the syntax wrong ? –  karthik A Feb 28 '12 at 6:49
show 1 more comment

Your Answer


