You are viewing limited content. For full access, please sign in.

Question

Question

Using Pattern Match to strip File Path Value

asked on January 12, 2024

Example : I have a Token value - /Folder1/Folder2/Folder3/Folder4/Folder5/File.pdf
There may be 0 or more folders in the Path.

I need to extract the Folder path (All except the File) and the File name.

End Result :
Path:  /Folder1/Folder2/Folder3/Folder4/Folder5/
File:   File.pdf 

I need to create two Patter Matching steps - One for the Path and one for the File.
Pattern matching Help needed please. 

0 0

Answer

SELECTED ANSWER
replied on January 12, 2024

It depends on how robust you need it to be and what might appear as possible inputs.  A fairly naïve, straightforward approach that might work for you is this:

Path
.*/
This regex says "capture everything, including slashes, until you find a slash" (and due to the fact that regex are "greedy" by default, it'll capture everything before the last slash).  If there are no slashes, the pattern will have no matches.  The .* says "0 or more characters" (including slashes), and the slash says there must be a literal slash character at the end.


File
[^/]+$
This pattern says "capture all text-that-is-not-a-slash and that is connected to the end of the string".  The [ ] mean "custom character class", in this case, we're saying "any character except slash" (the ^ means "except").  The + quantifier says there should be at least one character.  The $ is an "end of input" anchor--without it, the pattern matches "Folder1" in your example. 

 

Disclaimer: these patterns may match too much or too little, depending on what inputs they receive--it's impossible to make them more robust without more information (more inputs, assumptions that can be made, the source of the file paths, etc.).

4 0
replied on January 12, 2024 • Show version history

Once you have the path, another option for the File Name, instead of pattern matching, is to use functions and indexing.

If you split on the / character first (you could also remove empty items just in case to account for the possibility of a trailing slash) then you can take the last item with Apply Index.

2 0
replied on January 16, 2024

Jacob: Appreciate the information. Specifically, the detailed breakdown of each part of the Pattern.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.