I want to match just the folder name that a file is in,
eg:
pic/2009/cat01.jpg
pic/2009/01/cat02.jpg
I want to just match what I put in bold.
So far I have this:
[^/]*/
Which will match,
pic/2009/cat01.jpg
Any idea?
I want to match just the folder name that a file is in,
eg:
pic/2009/cat01.jpg
pic/2009/01/cat02.jpg
I want to just match what I put in bold.
So far I have this:
[^/]*/
Which will match,
pic/2009/cat01.jpg
Any idea?
Not sure I understand what you're asking, but try this:
[^/]+(?=/[^/]+$)
That will match the second to last section only.
Explanation:
(?x) # enable comment mode
[^/]+ # anything that is not a slash, one or more times
(?= # begin lookahead
/ # a slash
[^/]+ # again, anything that is not a slash, once or more
$ # end of line
) # end lookahead
The lookahead section will not be included in the match (group 0) - (you can omit the lookahead but include its contents if your regex engine doesn't do lookahead, then you just need to split on / and get the first item).
Hmmm... haven't done bash regex in a while... possibly you might need to escape it:
[^\/]+\(?=\/[^\/]+$\)
A regular expression like this should do the trick:
/\/([^\/]+)\/[^\/]+$/
The value you're after will be in the first capture group.
Without using a regular expression:
FILE_NAME="pic/2009/cat01.jpg"
basename $(dirname $FILE_NAME)
dirname
gets the directory part of the path, basename
prints the last part.
without the use of external commands or regular expression, in bash
# FILE_NAME="pic/2009/cat01.jpg"
# FILE_NAME=${FILE_NAME%/*}
# # echo ${FILE_NAME##*/}
2009