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

Min*_*int 2 regex bash shell

我想只匹配文件所在的文件夹名称,

如:
PIC/2009年 /cat01.jpg
PIC/2009/ 01 /cat02.jpg

我想把我用粗体添加的内容匹配.

到目前为止我有这个:

[^/]*/
Run Code Online (Sandbox Code Playgroud)

哪个会匹配,
pic/2009 / cat01.jpg

任何的想法?

Pet*_*ton 7

不确定我理解你的要求,但试试这个:

[^/]+(?=/[^/]+$)
Run Code Online (Sandbox Code Playgroud)

这将仅匹配倒数第二部分.


说明:

(?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
Run Code Online (Sandbox Code Playgroud)

前瞻部分不会包含在匹配中(组0) - (如果你的正则表达式引擎没有做前瞻,你可以省略前瞻但包括它的内容,那么你只需要拆分/得到第一项).

嗯......有一段时间没有完成bash正则表达式...可能你可能需要逃避它:

[^\/]+\(?=\/[^\/]+$\)
Run Code Online (Sandbox Code Playgroud)