从 URL 获取文件夹名称的正则表达式

regular expression to get folder name from URL

是否可以从 URL 中获取文件夹名称?

例如,

https://inside.nov.com/wh/pc/testSourceLib/Folder Z/SubFolder Z1/Copy files from one Document Library to another using Nintex Workflow _ My Learnings.pdf

其中

我需要从上面的 URL.

中提取 /Folder Z/Subfolder z1

我试过 ([^\/]*)$,它给了我文件名 Copy files from one Document Library to another using Nintex Workflow _ My Learnings.pdf

您可以使用基于前瞻性的正则表达式和 Extract 选项:

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

regex demo

图案详情:

  • [^/]+ - /...
  • 以外的 1 个或多个字符
  • (?=/[^/]*$) - 后跟 /、除 / 之外的零个或多个字符 ([^/]*),然后是字符串结尾 ($) .该构造是一个正向前瞻,它是一个零宽度断言,它不消耗文本(不将其放入返回的匹配项中),需要字符串中当前位置右侧的一些文本。

要稍微缩短模式,您可以用非捕获组包装 /[^/]+ 子模式,并在其上应用限制量词:

(?:/[^/]+){2}(?=/[^/]*$)

another demo