Search code examples
.netregexnintex-workflow

regular expression to get folder name from URL


Is it possible to get folder name from a URL?

For example,

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

where

  • https://inside.nov.com/wh/pc/ is the Web URL
  • testSourceLib is the library name
  • Folder Z/Subfolder z1 is the folder name

I need to extract /Folder Z/Subfolder z1 from the above URL.

I tried ([^\/]*)$ and it gives me the file name Copy files from one Document Library to another using Nintex Workflow _ My Learnings.pdf.


Solution

  • You can use a lookahead based regex with the Extract option:

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

    See the regex demo

    enter image description here

    Pattern details:

    • [^/]+ - 1 or more characters other than /...
    • (?=/[^/]*$) - followed with /, zero or more characters other than / ([^/]*) and then the end of string ($). The construct is a positive lookahead that is a zero-width assertion that does not consume the text (not putting it into the returned match) that requires some text to the right of the current location in the string.

    To shorten the pattern a bit, you may wrap the /[^/]+ subpattern with a non-capturing group and apply a limiting quantifier on it:

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

    See another demo