2

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Fuji - H2O
  • 367
  • 1
  • 5
  • 17

1 Answers1

2

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This gives me "SubFolder Z1" but I need "/Folder Z/SubFolder Z1/" to be the output – Fuji - H2O Nov 10 '16 at 20:21
  • Thank you Wiktor. You saved my day. Thanks Thanks Thanks. – Fuji - H2O Nov 10 '16 at 20:23
  • Glad to help. Note that in the string regex patterns where regex delimiters are not used, the `/` does not have to be escaped as it is not a regex special metacharacter. – Wiktor Stribiżew Nov 10 '16 at 20:26
  • Wictor, I have a question sometimes I only have 1 folder or 3 sub folders the expression works if I have 2 folders. What happens if I have more than 3 or 4 subfolders. – Fuji - H2O Nov 10 '16 at 20:26
  • That depends on what is static (=constant) in the pattern. If the starting substring is the same, use `(?<=https://inside.nov.com/wh/pc/testSourceLib)(?:/[^/]+)+(?=/[^/]*$)` – Wiktor Stribiżew Nov 10 '16 at 20:28
  • Yes, the stating url is static. For example below https://inside.nov.com/wh/pc/testSourceLib/Full Scope/VOPAK VI - 4902/Supporting Documents/Contract Value Change/Final Settlement Agreement/Vo Punch List Mechanical_ Civil — E — I.pdf I have 5 folders. – Fuji - H2O Nov 10 '16 at 20:30
  • See [this demo](http://regexstorm.net/tester?p=%28%3f%3c%3dhttps%3a%2f%2finside.nov.com%2fwh%2fpc%2ftestSourceLib%29%28%3f%3a%2f%5b%5e%2f%5d%2b%29%2b%28%3f%3d%2f%5b%5e%2f%5d*%24%29&i=https%3a%2f%2finside.nov.com%2fwh%2fpc%2ftestSourceLib%2fFull+Scope%2fVOPAK+VI+-+4902%2fSupporting+Documents%2fContract+Value+Change%2fFinal+Settlement+Agreement%2fVo+Punch+List+Mechanical_+Civil+%e2%80%94+E+%e2%80%94+I.pdf). – Wiktor Stribiżew Nov 10 '16 at 20:32
  • I truly appreciate it your help and willing to teach me the details. You ROCK...... THANKS AGAIN FOR YOUR TIME AND WILLING TO HELP SO QUICKLY. – Fuji - H2O Nov 10 '16 at 21:10