bash 使用正则表达式从 url 匹配文件夹名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1130016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Match folder name from url using regex
提问by Mint
I want to match just the folder name that a file is in,
我只想匹配文件所在的文件夹名称,
eg:
pic/2009/cat01.jpg
pic/2009/01/cat02.jpg
例如:
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
哪个会匹配,
pic/2009/cat01.jpg
Any idea?
任何的想法?
回答by Peter Boughton
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).
前瞻部分将不包含在匹配中(组 0)-(如果您的正则表达式引擎不进行前瞻,您可以省略前瞻但包括其内容,那么您只需要拆分 / 并获得第一项)。
Hmmm... haven't done bash regex in a while... possibly you might need to escape it:
嗯...有一段时间没有做 bash regex... 可能你可能需要逃避它:
[^\/]+\(?=\/[^\/]+$\)
回答by Paolo Tedesco
Without using a regular expression:
不使用正则表达式:
FILE_NAME="pic/2009/cat01.jpg"
basename $(dirname $FILE_NAME)
dirnamegets the directory part of the path, basenameprints the last part.
dirname获取路径的目录部分,basename打印最后一部分。
回答by ghostdog74
without the use of external commands or regular expression, in bash
不使用外部命令或正则表达式,在 bash 中
# FILE_NAME="pic/2009/cat01.jpg"
# FILE_NAME=${FILE_NAME%/*}
# # echo ${FILE_NAME##*/}
2009
回答by Mint
My lazy answer:
我懒惰的回答:
for INPUTS in pic/2009/cat01.jpg pic/2009/01/cat02.jpg ; do
echo "Next path is $INPUTS";
LFN="$INPUTS";
for FN in `echo $INPUTS | tr / \ ` ; do
PF="$LFN";
LFN="$FN";
done;
echo "Parent folder of $FN is $PF";
done;
回答by ExpertNoob1
echo pic/2009/cat01.jpg | awk -F/ '{print $(NF-1)}'
回声图片/2009/cat01.jpg | awk -F/ '{print $(NF-1)}'
回答by Adam
Try:
尝试:
/[a-z0-9_-]+
This would mark all folders in an URL string starting from / including folders having '_' or '-' in the folder name. Hope this would help.
这将标记 URL 字符串中从 / 开始的所有文件夹,包括文件夹名称中带有“_”或“-”的文件夹。希望这会有所帮助。
回答by Nathan de Vries
A regular expression like this should do the trick:
像这样的正则表达式应该可以解决问题:
/\/([^\/]+)\/[^\/]+$/
The value you're after will be in the first capture group.
您所追求的值将在第一个捕获组中。

