使用正则表达式匹配来自url的文件夹名称

时间:2022-02-15 23:40:21

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?

7 个解决方案

#1


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.

dirname获取路径的目录部分,basename打印最后一部分。

#2


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正则表达式...可能你可能需要逃避它:

[^\/]+\(?=\/[^\/]+$\)

#3


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

#4


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;

#5


echo pic/2009/cat01.jpg | awk -F/ '{print $(NF-1)}'

echo pic / 2009 / cat01.jpg | awk -F /'{print $(NF-1)}'

#6


A regular expression like this should do the trick:

这样的正则表达式可以解决这个问题:

/\/([^\/]+)\/[^\/]+$/

The value you're after will be in the first capture group.

您所追求的值将位于第一个捕获组中。

#7


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字符串中的所有文件夹,从/包含文件夹名称中包含“_”或“ - ”的文件夹开始。希望这会有所帮助。

#1


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.

dirname获取路径的目录部分,basename打印最后一部分。

#2


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正则表达式...可能你可能需要逃避它:

[^\/]+\(?=\/[^\/]+$\)

#3


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

#4


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;

#5


echo pic/2009/cat01.jpg | awk -F/ '{print $(NF-1)}'

echo pic / 2009 / cat01.jpg | awk -F /'{print $(NF-1)}'

#6


A regular expression like this should do the trick:

这样的正则表达式可以解决这个问题:

/\/([^\/]+)\/[^\/]+$/

The value you're after will be in the first capture group.

您所追求的值将位于第一个捕获组中。

#7


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字符串中的所有文件夹,从/包含文件夹名称中包含“_”或“ - ”的文件夹开始。希望这会有所帮助。